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;
I am a beginner with React. I am writing a TodoList. I already know that function need to bind in the component, so I use ES6 arrow function to do the same thing.
handleItemDelete has bind by using ES6 arrow function, however, the input index is not correct, sometimes it is not the correct index that it should be. I have no idea where goes wrong.
e.g.
todo1
todo2
todo3
If click todo3, todo1 disappear.