question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add oldProps as additional argument to getDerivedStateFromProps ?

See original GitHub issue

Do you want to request a feature or report a bug?

feature

What is the current behavior?

getDerivedStateFromProps only receives the nextProps and previousState as arguments.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

The deprecated componentWillReceiveProps(nextProps) used to allow code like this.props.foo !== nextProps.foo. With the new getDerivedStateFromProps function, there’s no choice (because it is a static method) but to constantly copy nextProps.foo into state in order to access it later.

This is illustrated in the example posted to twitter by @gaearon: https://twitter.com/dan_abramov/status/953612246634188800?lang=en

What is the expected behavior?

Ideally (if it’s not difficult to implement!) the getDerivedStateFromProps would also take the current (previous/old) props as an argument, something like:

getDerivedStateFromProps(nextProps, prevState, prevProps)

This would eliminate the need to constantly assign props to state purely for comparison purposes…

A quick look at the source doesn’t make it clear to me how easy this would be though…

https://github.com/facebook/react/blob/4a20ff26ecfe9bc66941d79f7fce2c67be8ee236/packages/react-dom/src/server/ReactPartialRenderer.js#L456

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

16.3.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
jharris4commented, Feb 8, 2018

Right, as I mentioned in my original post, I know the current API does enable this, but it requires mixing props into the state (I had linked to a similar example to the one you just provided).

I know it’s not the end of the world to mix the props into the state, but to me it has a bad code smell (particularly when you’re only mixing props into state for the sole use of this function).

I’d be curious to know why you think the prevProps parameter would be confusing, particularly since the use of it would be entirely optional.

Thanks for linking the RFC issue (https://github.com/reactjs/rfcs/issues/19), I hadn’t thought to look for discussion in the other repo! 😃

To summarize, we currently have a large number of React Components that currently utilize componentWillReceiveProps, and almost all of them compare several props with this.props.prop !== nextProps.prop.

When we migrate to using getDerivedStateFromProps we’ll now have to push each and every one of those props into state, which in my opinion is not ideal.

It also means we’ll have to go and sanity check the state of each of these components to make sure that we don’t accidentally end up with prop vs state key collisions, and we have to make sure we don’t mixup reading the key from state accidentally instead of props…

That last point is probably one of the best reasons I could put forward as to why I think it’d be cleaner (and arguably less confusing) to just pass the prevProps as an extra argument.

2reactions
jharris4commented, Feb 9, 2018

Ahhh! yeah, that was not obvious, it was indeed buried under the “show outdated”. Thanks for your patience while I got orientated! 😃

So I now know that by “confusion” you were referring to the initial call to getDerivedStateFromProps where the prevProps do not yet exist…

My vote would still be to do that rather than force pollution of the state, but having now read the original thread, I see that this concern was already raised and that you’ve nonetheless committed to the API as is.

Maybe I’ll look into creating some kind of utility HOC that at least removes the duplicated boilerplate required for moving props to state, and ideally even stores all stateProps under one state key to mitigate the risk of collisions. something like:

getDerivedStateFromProps(nextProps, prevState) {
  const  prevProps = prevState.__cloneProps;  
  if (!prevProps || prevProps.propA !== nextProps.propA || prevProps.propB !== nextProps.probB) {
    return {
      thingInState: "value",
      __cloneProps: cloneObjectWithKeys(nextProps, ['propA', 'propB'])
    };
  } 
  return null;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Compare with previous props in getDerivedStateFromProps
Here are the two examples of how you would use getDerivedStateFromProps compared to componentWillRecieveProps.
Read more >
How to Use getDerivedStateFromProps in React 16.3+
It takes in two arguments: the next props object (which may be the same ... some extra state duplicating your old props to...
Read more >
From the post it looks like getDerivedStateFromProps does not ...
From the post it looks like getDerivedStateFromProps does not receive the old props as an argument. I can think of cases where that...
Read more >
You Probably Don't Need Derived State – React Blog
The getDerivedStateFromProps bugfix in 16.4 makes derived state more predictable, so the results of misusing it are easier to notice. Note. All ...
Read more >
[Solved]-getDerivedStateFromProps, change of state under ...
Coding example for the question getDerivedStateFromProps, change of state under the ... (can add other conditionals limit number of calls to start, e.g., ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found