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
getDerivedStateFromProps is executed after setState #13015
Comments
It is expected. See for details: https://reactjs.org/blog/2018/05/23/react-v-16-4.html#bugfix-for-getderivedstatefromprops If this change breaks your code, it means your code already has a bug that causes state to reset too often. |
@gaearon thank you for the clarification. |
Simple question: what to do, if getDerivedStateFromProps is required just to update the state after the props are changed? |
I am confused: The https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html says: So why it's updated after the state change? |
TBH, I'm puzzled with this (not so new) It's been a while now since this API was released, we have few components that still use the UNSAFE In the blog post, the examples that demonstrate how this API can be avoided are way too simple. Last, why should anyone want to derive a state from state, if I've used |
I got one issue with some application that has legacy code. In order to achieve Another solution could rewrite the million line of code of a legacy application, and right now to do this is not the case (it would be done step by step). As @ethanshar mentioned, we not always writing simple code (combine both controlled and uncontrolled principles) and not always writing applications from scratch. |
using react 16.8.6 and had the same issue today, i know the issue is closed, but it is still does not make any sense that changing anything in internal state of component would call if this is supposed to work on both props and state change, why deprecate |
@gaearon Please, open the issue, as it is truly unexpected behavior. |
@gaearon Please, open the issue, as I am also facing this issue. |
please open! this is screwing with me too - based on the doc's saying either the docs need to be updated to better describe the purpose of this method, or the method needs to be updated to better reflect the docs |
same issue here |
This comment has been minimized.
This comment has been minimized.
In case anyone is still wondering, this makes the most sense to me. Since |
Do you want to request a feature or report a bug?
BUG
What is the current behavior?
Whenever the state is update via setState method, getDerivedStateFromProps is also executed even if the props have not changed.
I'm not sure if this is a bug, if it's not, this is kind of unexpected and it's not clear in the post you've published about it.
See this JSFiddle, I'm not sure which version of React is used in here, but it's reproducible.
What is the expected behavior?
When I update the state via setState, getDerivedStateFromProps should not be executed unless any prop has changed.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React: 16.4.0
node -v: v8.9.4
npm -v: 5.6.0
Operating system: Windows 10
Browser and version: Chrome 66.0.3359.181 (Official Build) (64-bit)
The text was updated successfully, but these errors were encountered: