Skip to content

Instantly share code, notes, and snippets.

@amanshu-kataria
Created May 14, 2018 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amanshu-kataria/92fe6bb6962962a0a45abaee4cb4dfa0 to your computer and use it in GitHub Desktop.
Save amanshu-kataria/92fe6bb6962962a0a45abaee4cb4dfa0 to your computer and use it in GitHub Desktop.
static getDerivedStateFromProps(nextProps, prevState){
if(nextProps.someValue!==prevState.someValue){
return { someState: nextProps.someValue};
}
else return null;
}
componentDidUpdate(prevProps, prevState) {
if(prevProps.someValue!==this.props.someValue){
//Perform some operation here
this.setState({someState: someValue});
this.classMethod();
}
}
@ProiOSDeveloper
Copy link

this.setState({someState: someValue});
shouldn't this be:
this.setState({ someState: this.props.someValue }); ?

@Austinove
Copy link

Hi this is good, but how can i mutate the state

@Austinove
Copy link

Any way i got it thanks @ProiOSDeveloper

@RagabMohamedRagab
Copy link

this.setState({someState: someValue});
shouldn't this be:
this.setState({ someState: this.props.someValue }); ?

@amanshu-kataria
Copy link
Author

@ragabmoh anything is fine. It's just for purpose of example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment