import React, { Component, Fragment } from 'react';
import TodoItem from './TodoItem';
import './style.css';
class TodoList extends Component {
constructor(props) {
console.log(`constructor`);
super(props);
this.state = {
inputValue: '',
list: []
}
this.handleInputChange = this.handleInputChange.bind(this);
this.handleBtnClick = this.handleBtnClick.bind(this);
this.handleItemDelete = this.handleItemDelete.bind(this);
}
UNSAFE_componentWillMount() {
console.log(`componentWillMount`);
}
componentDidMount() {
console.log(`componentDidMount`);
}
shouldComponentUpdate() {
console.log(`shouldComponentUpdate`);
return true;
}
UNSAFE_componentWillUpdate() {
console.log(`componentWillUpdate`);
}
componentDidUpdate() {
console.log(`componentDidUpdate`);
}
UNSAFE_componentWillReceiveProps() {
console.log(`componentWillReceiveProps`)
}
componentWillUnmount(){
console.log(`componentWillUnmount`);
}
render() {
console.log(`render`);
return (
<Fragment>
<div>
<label htmlFor="insertArea">请输入内容</label>
<input
id="insertArea"
className="input"
value={this.state.inputValue}
onChange={this.handleInputChange}
ref={(input) => { this.input = input }}
/>
<button onClick={this.handleBtnClick}>提交</button>
</div>
<ul>
{this.getTodoItem()}
</ul>
</Fragment>
)
}
getTodoItem() {
return this.state.list.map((item, index) => {
return (
<div key={index}>
<TodoItem content={item} index={index} handleItemDelete={this.handleItemDelete} />
{ }
</div>
)
})
}
handleInputChange(e) {
let inputValue = this.input.value;
this.setState(() => ({ inputValue }), () => {
});
}
handleBtnClick(e) {
this.setState((prevState) => ({ inputValue: '', list: [...prevState.list, prevState.inputValue] }));
}
handleItemDelete(index) {
this.setState((prevState) => {
let list = [...prevState.list];
list.splice(index, 1);
return { list }
});
}
}
export default TodoList;