单击提交按钮时,父组件中的 SetState 未更新。将方法作为 prop 发送给子进程,以验证 ReactJS 中的输入控件

发布于 2025-01-13 11:29:08 字数 1591 浏览 0 评论 0原文

我创建了类组件 - 一个系列组件,它是父组件,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文