'SetState is not getting updated in parent component on submit button click. Sends method as prop to child which validates the input control in reactjs

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source