React introduced new static method getDerivedStateFromProps(props, state)
which is called before every render method, but why? Calling it after prop change makes se
For this case (updating the state based on props change), use:
componentDidUpdate(prevProps) {
// don't forget to compare props
if (this.props.userID !== prevProps.userID) {
this.fetchData(this.props.userID);
}
}
componentDidUpdate
will get called after each update (due to props changes / state changes). so you should check if the prop is changed (by this.props.userID !== prevProps.userID
).