Skip to content Skip to sidebar Skip to footer

Calling Setstate On A Parent Component Forces Componentdidupdate On Its Children?

I have two components, parent component App and child component SearchBar, I want SearchBar to keep its own state, and after updating its state to call a function given by its pare

Solution 1:

componentDidUpdate provides two arguments prevProps and prevState, with it you can check if actual props or state has changed and then make changes to current state/props.. for example:

componentDidUpdate(prevProps, prevState){
if(prevState.searchTerm !== this.state.searchTerm) {
   this.props.onSearchChange(this.state.searchTerm)
  }
}

Post a Comment for "Calling Setstate On A Parent Component Forces Componentdidupdate On Its Children?"