import React, { Component } from 'react';
import { Button } from 'antd';
import { Menu, Dropdown, Icon } from 'antd';
import { Input } from 'antd';
import { List } from 'antd';
class TodoList extends Component {
state = {
input: '',
list: ['todo1', 'todo2']
}
render() {
return (
<div>
<Input
onChange={this.handleInputChange}
value={this.state.input}
/>
<Button
type="primary"
onClick={this.handleBtnClick}
>
click me
</Button>
<ul>
{
this.state.list.map((item, index)=> {
return (
<li key={index}
onClick={this.handleItemDelete}
>
{item}
</li>
)
})
}
</ul>
</div>
);
}
handleInputChange = (e)=>{
this.setState({
input: e.target.value
})
}
handleBtnClick = ()=>{
this.setState({
list: [...this.state.list, this.state.input],
input: ''
})
}
handleItemDelete = (index)=>{
const list = [...this.state.list]; // copy
list.splice(index, 1); // start from index, delete one element
this.setState({
list: list
})
}
}
export default TodoList;
我是 React 的初学者。我正在写一个 TodoList。我已经知道该函数需要在组件中绑定,所以我使用 ES6 箭头函数来做同样的事情。
handleItemDelete 使用 ES6 箭头函数进行绑定,但是输入的索引不正确,有时它不是正确的索引。我不知道哪里出了问题。
例如
待办事项1
待办事项2
待办事项3
如果单击 todo3,则 todo1 消失。