简体   繁体   English

状态被清除,但是在React中提交表单后,输入字段文本不存在

[英]state is cleared, but input field text is not after form is submitted in React

I am doing exercise on a simple todo App, the user can type their todos in the field, and hit submit to see it added to the todo-list. 我正在一个简单的待办事项应用程序上进行练习,用户可以在字段中键入待办事项,然后单击提交以将其添加到待办事项列表中。

I've managed to clear the state once the form is submitted with 'this.setState({newTodo: ''})' (indicated by hitting submit again will add an empty todo-item); 一旦使用'this.setState({newTodo:''}))'提交表单,我就设法清除了状态(再次点击Submit会添加一个空的待办事项);

however, the text in the input field will not be cleared. 但是,输入字段中的文本不会被清除。

const TodoItem = ({ text }) => <li>{text}</li>;

class App extends Component {
constructor(props) {
    super(props);

    this.state = {
        todos: ['walk dog', 'feed cat'],
        newTodo: ''
};

this.handleSubmit = this.handleSubmit.bind(this);
}

handleSubmit(e) {
    e.preventDefault();

    const todos = [...this.state.todos, this.state.newTodo];

    this.setState({ todos, newTodo: '' });
}

render() {
    const { newTodo } = this.state; 
    const todos = this.state.todos.map((todo, index) => <TodoItem key={index} text={todo} />);

    return (
        <div className="App">
            <form onSubmit={this.handleSubmit}>
                <h1>Simple Todo App</h1>

                <input
                    type="text"
                    name="newTodo"
                    value={this.newTodo}

                    onChange={e => this.setState({ [e.target.name]: e.target.value })}
                />

                <ol>
                  {todos}
                </ol>
                <button>SAVE</button>
            </form>
        </div>
    );
}
}

export default App;

Thanks for any kind of help. 感谢您的任何帮助。

this.newTodo is undefined, use this.state.newTodo instead od this.newTodo : this.newTodo是未定义的,请使用this.state.newTodo代替this.newTodo

<input
     type="text"
     name="newTodo"
     value={this.state.newTodo} 
      onChange={e => this.setState({ [e.target.name]: e.target.value })}
/>

OR: 要么:

const { newTodo } = this.state; 
<input
     type="text"
     name="newTodo"
     value={newTodo} 
      onChange={e => this.setState({ [e.target.name]: e.target.value })}
/>

The reason you see empty newTodo added because your newTodo initial state is empty and in handleSubmit you are always passing it irrespective of whether it's is empty or not. 之所以会看到添加了空newTodo的原因是因为newTodo的初始状态为空,并且在handleSubmit中,无论它是否为空,您总是会传递它。 So In handleSubmit check newTodo state and then add newTodo to todos array. 因此,在handleSubmit中检查newTodo状态,然后将newTodo添加到todos数组中。

 if(this.state.newTodo != “”){
      const todos = [...this.state.todos, this.state.newTodo];
 }

and change input field value attribute value to newTodo 并将输入字段值属性值更改为newTodo

 <input value={newTodo} />

Don't use this.newTodo 不要使用this.newTodo

In the following section: 在以下部分中:

<input
 type="text"
 name="newTodo"
 value={this.newTodo}
 onChange={e => this.setState({ [e.target.name]: e.target.value })}
/>

Change value={this.newTodo} to value={this.state.newTodo} value={this.newTodo}更改为value={this.state.newTodo}

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

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