You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importReactfrom'react'importReactDOMfrom'react-dom'importTodofrom'./Todo'classAppextendsReact.Component{constructor(props){super(props)this.state={value: '',todos: [],id: 0}this.handleClick=this.handleClick.bind(this)// [1-2]this.handleChange=this.handleChange.bind(this)this.removeTodo=this.removeTodo.bind(this)this.id=0}handleClick(){console.log(this)// [1-1]const{ todos, value }=this.statethis.setState({todos: [
...todos,{text: value,id: this.id++}],value: ''})}handleChange(e){this.setState({value: e.target.value})}removeTodo(todo){const{ todos }=this.statethis.setState({todos: todos.filter(item=>item.id!==todo.id)})}render(){console.log('App Render!')const{ todos, value }=this.statereturn(<div><h1>Todo list React Version</h1><label>todo:
<inputvalue={value}onChange={this.handleChange}/></label><buttononClick={this.handleClick}>Add Todo</button><ulclassName="todolist">{todos.map(todo=><Todokey={todo.id}todo={todo}removeTodo={this.removeTodo}/>)}</ul></div>)}}ReactDOM.render(<App/>,document.getElementById('root'))
Todo.js
importReactfrom'react'classTodoextendsReact.Component{constructor(props){super(props)this.handleDelete=this.handleDelete.bind(this)}shouldComponentUpdate(nextProps){if(nextProps.todo===this.props.todo)returnfalseelsereturntrue}handleDelete(){const{ todo, removeTodo }=this.propsremoveTodo(todo)}render(){console.log('Todo Render')const{ todo }=this.propsreturn(<li>{todo.id}: {todo.text}<buttononClick={this.handleDelete}>X</button></li>)}}exportdefaultTodo
The text was updated successfully, but these errors were encountered:
功能:新增、刪除清單內容
網址連結:待補
index.js
Todo.js
The text was updated successfully, but these errors were encountered: