-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Legacy lifecycle use-case for componentWillReceiveProps #729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think this may be a duplicate of #721 |
Yeah that's a real duplicate. Will close this. |
If you just need to perform a side effect in response to a prop change, you can do so from If you need to derive state from a comparison of previous and current props, please put the previous prop value in the state. Yes, it’s a bit more verbose, but it lets React hold onto less memory in the future in some cases (no need to keep the whole “previous props” object), and avoids the need to check class TabContainer extends Component {
state = {
selectedTab: 0,
prevTabs: null
};
static getDerivedStateFromProps(props, state) {
if (props.tabs !== state.prevTabs) {
return {
selectedTab: 0,
prevTabs: props.tabs
};
}
return null;
}
renderTab = (tab, index) => {
const active = this.state.selectedTab === index;
return (/* tab depending on active state */);
};
render() {
return (<div>
<ul>
{ this.props.tabs.map(this.renderTab) }
</ul>
{ /* selected tab content */ }
</div>);
}
} I know you’re saying |
Ah, this also answers my dilemmas from #721 (comment) I also see that both the blog post and your example make a point of naming props-in-state things like prevXXX or lastXXX, which I think is a nice touch. |
@gaearon thanks for the clarification, that it's actually the recommended way to plainly move a property to state, if we need to compare on it. I think it would be a good addition to the blog post (maybe as a subparagraph of the actual props to state example). Also your example above lacks setting the |
Oops, fixed! Thanks. |
In your update on async rendering blog post are a lot of examples on how to replace legacy lifecycle use-cases by the new methods. I am lacking an example for the following use-case: Change state depending on prop changes/diffs.
In contrast to the "Updating
state
based onprops
" which just looks into the new props and the previous state, I would like to see an example for a state change, that will require to look into the new and old properties - or an explanation why this is an anti-pattern.A typical use-case I've seen often (and also used myself often) is, that I check within
componentWillReceiveNewProps
whether a specific property has changed from previous properties to new and if so change a state. I didn't save the previous property in the state (which I see how that would work withgetDerivedStateFromProps
, because it's never changed within the component or considered a "state" of the component).A simple example of a tab container:
The text was updated successfully, but these errors were encountered: