源码
import React, { Component, Fragment } from "react";
class TodoList extends Component {
constructor(props) {
super(props);
this.state = {
inputValue: "",
list: ["Learn React", "Play LOL"]
};
}
render() {
return (
<Fragment>
<div>
<input
value={this.state.inputValue}
onChange={this.handleInPutChange.bind(this)}
onChange={this.handleInPutChange.bind(this)}
/>
<button onClick={this.handleBtnClick.bind(this)}>Submit</button>
</div>
<ul>
{this.state.list.map((item, index) => {
return (
<li key={index} onClick={this.handleItemDelete.bind(this, index)}>
{item}
</li>
);
})}
</ul>
</Fragment>
);
}
handleBtnClick(e) {
this.setState({
list: [...this.state.list, this.state.inputValue],
inputValue: ""
});
}
handleInPutChange(e) {
this.setState({
inputValue: e.target.value
});
}
handleItemDelete(index) {
const list = [...this.state.list];
list.splice(index, 1);
this.setState({
list: list
});
}
}
export default TodoList;