Reactjs 更新React onSubmit中的道具

Reactjs 更新React onSubmit中的道具,reactjs,forms,submit,react-props,Reactjs,Forms,Submit,React Props,我有这个Ant设计表单,目前我一直在输出错误消息:在下面附加的代码块中,handleSubmit没有响应。我希望在提交错误输入后立即看到错误消息“用户名和密码不匹配”,但该消息仅在其中一个字段更改后显示(我有多个字段,但我只是为了保持清晰而设置了此字段) 请注意,“更新道具”是在我提交错误数据后立即记录的,但错误消息仅在对另一个字段进行额外更改后才会更改 类NormalLoginForm扩展了React.Component{ 建造师(道具){ 超级(道具); this.state={userna

我有这个Ant设计表单,目前我一直在输出错误消息:在下面附加的代码块中,
handleSubmit
没有响应。我希望在提交错误输入后立即看到错误消息“用户名和密码不匹配”,但该消息仅在其中一个字段更改后显示(我有多个字段,但我只是为了保持清晰而设置了此字段)

请注意,“更新道具”是在我提交错误数据后立即记录的,但错误消息仅在对另一个字段进行额外更改后才会更改

类NormalLoginForm扩展了React.Component{
建造师(道具){
超级(道具);
this.state={username:'',usernameProps:{},};
this.handleSubmit=this.handleSubmit.bind(this);
}
handleSubmit(事件){
event=>event.preventDefault();
postData('/account/login',this.state)
。然后(数据=>{
if(data.success){window.location='/';}
否则{
this.usernameProps={
hasFeedback:true,validateStatus:“错误”,
帮助:“用户名和密码不匹配”
}
console.log(‘更新道具’);//这是日志
}
});
}
渲染(){
返回(
登录
)
}
};
render(,document.getElementById(“根”));

谁能告诉我哪里做错了?非常感谢。

在更新状态时,请使用setState方法,避免像您在此处所做的那样直接更新状态

 this.usernameProps = {
      hasFeedback: true, validateStatus: "error",
      help: "Username and Password do not match"
 }
这样做:

const userNameState = {...this.state.usernameProps, hasFeedback: true, validateStatus: "error",
                      help: "Username and Password do not match"};
this.setState({usernameProps: userNameState});

您可以在表单中使用此状态作为
this.state.usernameProps

,在表单中将usernameProps作为this.state.usernameProps传递