简体   繁体   English

Todo React JS应用程序1

[英]Todo React JS App 1

Need to create a React app that would display a text input for the user, where they will be able to write a task with a button displayed beside the input. 需要创建一个React应用程序,该应用程序将为用户显示文本输入,他们将能够使用输入旁边显示的按钮来编写任务。 When clicked, this button should add the user's input to a list of to do items. 单击时,此按钮应将用户输入添加到待办事项列表中。 Blank items should not be added to the list. 空白项目不应添加到列表中。 Each list item must display a checkbox, and the task that the user wrote. 每个列表项必须显示一个复选框,以及用户编写的任务。 When checking off the checkbox, the list item should be removed from the list. 选中该复选框时,应从列表中删除该列表项。 Also I'm getting the following error: error message My code: 我也收到以下错误: 错误消息我的代码:

 App.js: import React, { Component } from 'react'; import MyList from './MyList.js'; class App extends Component { constructor(props) { super(props); this.state = { task }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(e) { const name = e.target.name; const value = e.target.type === 'checkbox' ? e.target.checked : e.target.value; this.setState({ [name]: value }); } handleSubmit(e) { e.preventDefault(); console.log('SUBMITTED', this.state); } render() { return ( <div className="App"> <MyList name="task" currentTask={this.state.task} onSubmit={this.handleSubmit} onChange={this.handleChange} /> </div> ); } } export default App; MyList.js: import React, { Component } from 'react'; import App from "./App.js"; class MyList extends Component { constructor (props) { super(props); this.state = { task }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(e) { const name = e.target.name; const value = e.target.type === 'checkbox' ? e.target.checked : e.target.value; this.setState({ [name]: value }); } handleSubmit(e) { e.preventDefault(); console.log('SUBMITTED', this.state); } render () { return ( <div> My Task List <input type="text" name="task" /> <button onClick={this.handleSubmit}>Add</button> <br /> { this.handleChange } </div > ); } } export default MyList; 

First, you need to initialize the task state with an initial value that can be null or an empty string like so: 首先,您需要使用可以为null或空字符串的初始值初始化任务状态,如下所示:

this.state = { task : '' }

secondary, you must put the this.handleChange method on the onChange of the input like : 第二,必须将this.handleChange方法放在输入的onChange上,例如:

<input type="text" name="task" onChange={this.handleChange} />.

check the code for more details: CodeSandBox 检查代码以获取更多详细信息: CodeSandBox

 class ListItem extends React.Component { render (){ return( <li> <input type="checkbox" onChange={this.props.onCheckBoxChange} /> <span>{this.props.text}</span> </li> ) } } class MyList extends React.Component { render() { return ( <ul> {this.props.tasks.map(task => { return <ListItem key={task.id} text={task.text} onCheckBoxChange={()=>this.props.deleteListItem(task.id)} />; })} </ul> ); } } class App extends React.Component { constructor(props) { super(props); this.state = { tasks : [] }; this.input = null; } addItemList(e){ // APPEND THE ITEM TO THE TASKS ARRAY this.setState((prevState) => ({ tasks: [ ...prevState.tasks, { id : new Date().getTime(), text : this.input.value } ] })) } deleteItem(itemId){ this.setState((prevState)=>({ tasks: prevState.tasks.filter((item)=>item.id !== itemId) })) } render() { return ( <div className="App"> <div> My Task List <input ref={(ref)=>{this.input = ref}} type="text" /> <button onClick={this.addItemList.bind(this)}>Add</button> </div> <MyList tasks={this.state.tasks} deleteListItem={(itemId)=>this.deleteItem(itemId)} /> </div> ); } } const styles = { fontFamily: 'sans-serif', textAlign: 'center', }; ReactDOM.render(<App />, document.getElementById('root')); 
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM