【发布时间】:2020-10-14 03:31:09
【问题描述】:
我有这门课。
class Demo extends React.Component {
constructor(props) {
super(props);
this.state = {
items: []
};
this.add = this.add.bind(this);
this.clear = this.clear.bind(this);
}
add() {
this.setState(prev => {
const n = prev.items.length;
return {
items: [<li key={n}>Hello, World {n}!</li>, ...prev.items]
};
});
}
clear() {
this.setState({ items: [] });
}
render() {
return (
<div>
<div>
<button onClick={this.add}>Add</button>
<button onClick={this.clear}>Clear</button>
</div>
{/* This is wrong, not sure what to do though... */}
<Collapse in={this.state.items.length > 0}>
<ul>{this.state.items}</ul>
</Collapse>
</div>
);
}
}
沙盒链接:https://codesandbox.io/s/material-demo-ggv04?file=/Demo.js
我试图做到这一点,以便每次单击“添加”按钮时,都会在列表顶部生成一个新项目,并且将现有项目下推。不知道如何继续。
额外资源
- 我想要实现的示例:https://codeburst.io/yet-another-to-do-list-app-this-time-with-react-transition-group-7d2d1cdf37fd
- React Transition Group 过渡文档:http://reactcommunity.org/react-transition-group/transition(似乎由
Collapse内部使用)
【问题讨论】:
标签: javascript reactjs material-ui