Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么React在更改时更新状态,而不是在提交(输入)时更新状态?_Javascript_Reactjs_Forms - Fatal编程技术网

Javascript 为什么React在更改时更新状态,而不是在提交(输入)时更新状态?

Javascript 为什么React在更改时更新状态,而不是在提交(输入)时更新状态?,javascript,reactjs,forms,Javascript,Reactjs,Forms,请参阅下面的代码: App.jsx addTodo = event => { this.setState({ newTodo: event.target.value, todos: [...this.state.todos, this.state.newTodo] }); console.log(this.state.todos); }; <form onSubmit={this.props.preventDef

请参阅下面的代码:

App.jsx

addTodo = event => {
    this.setState({
      newTodo: event.target.value,
      todos: [...this.state.todos, this.state.newTodo]
    });

    console.log(this.state.todos);
  };
        <form onSubmit={this.props.preventDefault} className="ui form">
          <div className="field">
            <label>Add a todo here:</label>
            <input
              onChange={this.props.addTodo} // addTodo works for onChange why not onSubmit?
              type="text"
              placeholder="Walk the dog..."></input>
            <button type="submit">Submit</button>
          </div>
        </form>
AddTodo.jsx

addTodo = event => {
    this.setState({
      newTodo: event.target.value,
      todos: [...this.state.todos, this.state.newTodo]
    });

    console.log(this.state.todos);
  };
        <form onSubmit={this.props.preventDefault} className="ui form">
          <div className="field">
            <label>Add a todo here:</label>
            <input
              onChange={this.props.addTodo} // addTodo works for onChange why not onSubmit?
              type="text"
              placeholder="Walk the dog..."></input>
            <button type="submit">Submit</button>
          </div>
        </form>

在此处添加待办事项:
提交

该函数按照预期更新onChange的状态,但对onSubmit没有更新,这是为什么?我怀疑它与它所在的标签有关(输入而非表单)。

preventDefault
阻止表单执行默认操作,即提交表单。对于
preventDefault

您需要编写一个自定义函数来处理希望表单执行的操作,并将
onSubmit
设置为该函数。在该自定义函数中,您可以使用
event.preventDefault
来阻止表单最初执行其默认操作。然后,在该函数的后面部分,您可以编写您需要执行的任何代码

以下是来自以下方面的示例:

类名称表单扩展了React.Component{
建造师(道具){
超级(道具);
this.state={value:''};
this.handleChange=this.handleChange.bind(this);
this.handleSubmit=this.handleSubmit.bind(this);
}
手变(活动){
this.setState({value:event.target.value});
}
handleSubmit(事件){
警报(“已提交名称:”+this.state.value);
event.preventDefault();
}
render(){
返回(
姓名:
);
}
}

此外,请查看并了解上面所示示例(受控组件方法)与非受控组件方法之间的区别。

preventDefault
阻止表单执行其默认操作,即提交表单。对于
preventDefault

您需要编写一个自定义函数来处理希望表单执行的操作,并将
onSubmit
设置为该函数。在该自定义函数中,您可以使用
event.preventDefault
来阻止表单最初执行其默认操作。然后,在该函数的后面部分,您可以编写您需要执行的任何代码

以下是来自以下方面的示例:

类名称表单扩展了React.Component{
建造师(道具){
超级(道具);
this.state={value:''};
this.handleChange=this.handleChange.bind(this);
this.handleSubmit=this.handleSubmit.bind(this);
}
手变(活动){
this.setState({value:event.target.value});
}
handleSubmit(事件){
警报(“已提交名称:”+this.state.value);
event.preventDefault();
}
render(){
返回(
姓名:
);
}
}

此外,请查看并理解我上面展示的示例(受控组件方法)之间的差异以及非受控组件方法。

根据您在提交时必须发生的事情?将输入值添加到state@AnuragSrivastava根据您在提交时必须发生的事情?将输入值添加到state@AnuragSrivastava