单击提交按钮时,父组件中的 SetState 未更新。将方法作为 prop 发送给子进程,以验证 ReactJS 中的输入控件
我创建了类组件 - 一个系列组件,它是父组件,Profile 作为子组件。
我曾经将方法作为 prop 和 onSubmit 单击发送给子组件,我调用此方法来验证子组件中的输入控件,并将 false 值作为参数发送给父组件 prop 方法,以因错误而停止,并且在 State of 中更新 IsValid 属性父组件为 false。
但这个状态没有得到更新。 我还绑定了父组件中的方法。
IsValidFamilyMember = (IsValidFamily) => { // IsValidFamily returns false from child
this.setState({IsValidFamily1: IsValidFamily }); // IsValidFamily1 should set to false
}
class Profile extends Form {
state: {IsValidFamily1: true}
}
doSubmit = async () => {
var isFamilyValid = true;
isFamilyValid = this.family.current != null ? this.validateFamily() : true;
}
validateFamily() {
this.family.current.setErrorInState(this.state.data.FamilyDetail); //This will call child(Family component Method)
}
//Child class Family
class Family extends Form {
setErrorInState = (data) => {
//some Logic
this.setIsValidFamily(false, errors); // false is what I will return
}
}
setIsValidFamily = async(IsValidFamily, errors) => {
this.props.IsValidFamily(IsValidFamily); // will call a parent method
}
//below is the parent method which will be call just after above method
IsValidFamilyMember = async(IsValidFamily) => { //IsValidFamily is returning false from child method
this.setState((prevState) => ({ ...prevState, IsValidFamily1: IsValidFamily })); // This is not updating state in Parent Component(profile component)
}
但最初声明为 true 的 IsValidFamily1 并未更新为 false 状态没有更新
I have created class components - a family component which is a parent component and Profile as child component.
I used to send method to child as prop and onSubmit click, I calls this to validate the input controls in child component and sends false value as parameter to parent component prop method to stop because of error and in that I updates IsValid property in State of Parent component as false.
But this state is not getting updated.
I have also bind the methods in parent component.
IsValidFamilyMember = (IsValidFamily) => { // IsValidFamily returns false from child
this.setState({IsValidFamily1: IsValidFamily }); // IsValidFamily1 should set to false
}
class Profile extends Form {
state: {IsValidFamily1: true}
}
doSubmit = async () => {
var isFamilyValid = true;
isFamilyValid = this.family.current != null ? this.validateFamily() : true;
}
validateFamily() {
this.family.current.setErrorInState(this.state.data.FamilyDetail); //This will call child(Family component Method)
}
//Child class Family
class Family extends Form {
setErrorInState = (data) => {
//some Logic
this.setIsValidFamily(false, errors); // false is what I will return
}
}
setIsValidFamily = async(IsValidFamily, errors) => {
this.props.IsValidFamily(IsValidFamily); // will call a parent method
}
//below is the parent method which will be call just after above method
IsValidFamilyMember = async(IsValidFamily) => { //IsValidFamily is returning false from child method
this.setState((prevState) => ({ ...prevState, IsValidFamily1: IsValidFamily })); // This is not updating state in Parent Component(profile component)
}
But IsValidFamily1 which is initially declared as true is not updated to false
State is not getting updated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论