Flora
Flora

Reputation: 563

React with Redux: Call a function after updating the store

How to make sure that the store is already updated and I can invoke a function that needs the updated store?

Upvotes: 0

Views: 1179

Answers (2)

Flora
Flora

Reputation: 563

I used shouldComponentUpdate

shouldComponentUpdate(nextProps) {
  if(this.props.someArray.length !== nextProps.someArray.length) {
    /* some function here */
    return true;
  }
  return false;
} 

Upvotes: 0

Sakhi Mansoor
Sakhi Mansoor

Reputation: 8102

Once you update the store. You can get updated global state within connected component in componentWillReceivePropslike this :

componentWillReceiveProps(nextProps){
  //invoke function with updated store
  //this.foo(nextProps)
  console.log(this.props); // prevProps
  console.log(nextProps); // currentProps after updating the store
  }

And you can also use getDerivedStateFromProps

 static getDerivedStateFromProps(nextProps, prevState) {
   }

Upvotes: 2

Related Questions