Skip to content
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

Closed
fcaballero opened this issue Jun 11, 2018 · 13 comments
Closed

getDerivedStateFromProps is executed after setState #13015

fcaballero opened this issue Jun 11, 2018 · 13 comments

Comments

@fcaballero
Copy link

fcaballero commented Jun 11, 2018

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)

@gaearon
Copy link
Collaborator

gaearon commented Jun 11, 2018

It is expected. See for details:

https://reactjs.org/blog/2018/05/23/react-v-16-4.html#bugfix-for-getderivedstatefromprops
https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html

If this change breaks your code, it means your code already has a bug that causes state to reset too often.

@gaearon gaearon closed this as completed Jun 11, 2018
@fcaballero
Copy link
Author

@gaearon thank you for the clarification.

@minbelapps
Copy link

Simple question: what to do, if getDerivedStateFromProps is required just to update the state after the props are changed?

@minbelapps
Copy link

It is expected. See for details:

https://reactjs.org/blog/2018/05/23/react-v-16-4.html#bugfix-for-getderivedstatefromprops
https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html

If this change breaks your code, it means your code already has a bug that causes state to reset too often.

I am confused: The https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html says: getDerivedStateFromProps exists for only one purpose. It enables a component to update its internal state as the result of changes in props.

So why it's updated after the state change?

@ethanshar
Copy link

TBH, I'm puzzled with this (not so new) getDerivedStateFromProps API.
As its name suggests, it should derive state from props, not from props & state.

It's been a while now since this API was released, we have few components that still use the UNSAFE componentWillReceiveProps which I'm not sure yet how we should migrate.

In the blog post, the examples that demonstrate how this API can be avoided are way too simple.
IRL, you have more complex cases which unfortunately combine both controlled and uncontrolled behaviors.
I understand those are considered a bad practice, but if until now we had some way to achieve these implementations using getDerivedStateFromProps, Now, with the recent update it is just impossible.

Last, why should anyone want to derive a state from state, if I've used setState in my code, I would include the derived state in that setState call to begin with.

@minbelapps
Copy link

I got one issue with some application that has legacy code. In order to achieve getDerivedStateFromPropsworking correctly, I implemented this component as old componentWillReceiveProps , because the state changes had the priority against props changes as the state was controlled by user actions. To achieve this, I stored previous props and did additional checkings to know if the state is changed. This solution is ridiculous with addional not necessary code and complexity. Ir reminds me of old (1.6) Angular.

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.

@Samnan
Copy link

Samnan commented Oct 13, 2019

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 getDerivedStateFromProps

if this is supposed to work on both props and state change, why deprecate componentWillReceiveProps?

@bogomazov
Copy link

bogomazov commented Oct 23, 2019

@gaearon Please, open the issue, as it is truly unexpected behavior.

@ronaklalwaniii
Copy link

@gaearon Please, open the issue, as I am also facing this issue.

@stopitdan
Copy link

please open!

this is screwing with me too - based on the doc's saying getDerivedStateFromProps exists for only one purpose. It enables a component to update its internal state as the result of changes in props. I am super confused as to why it would be called when state updates?

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

@tianzhich
Copy link

same issue here

@jiyarong

This comment has been minimized.

@philipfabianek
Copy link

In case anyone is still wondering, this makes the most sense to me. Since getDerivedStateFromProps depends on the old state, it only makes sense that it is called after a state change as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests