redux-devtools
redux-devtools copied to clipboard
Update unsafe_componentWillReceiveProps react-json-tree
This PR attempts to solve #635.
unsafe_componentWillReceiveProps
is currently used in two components: JSONTree
and JSONNestedNode
.
In JSONTree
I extracted the properties theme
and invertTheme
from Props
to a separate interface and extended the State
and Props
interfaces with it. The reason being, that the new static method getDerivedStateFromProps
does not have access to the current props, so properties that need to be compared should be mirrored in the local state object.
In JSONNestedNode
there is no need to check for derived state at all. The property expanded
can be copied to state and re-rendering is checked anyway in the shouldComponentUpdate
lifecycle hook.
I'm looking forward to feedback and will be glad to refactor if needed.
@BrunoFenzl Thanks for making a PR!
-
JSONNestedNode
:getStateFromProps
is now only ever called in theconstructor
. Before this PR it was called on every render. I think you might have to usegetDerivedStateFromProps
in order to match the previous behavior unless I'm missing something. In the future I would like to refactor it to removeexpanded
fromstate
altogether, but I don't think that's possible without callingshouldExpandNode
twice sinceshouldComponentUpdate
depends on it (which would be a breaking change). -
JSONTree
: The React team recommends not copying props to state just to memoize a value. I would prefer usingmemoize-one
as suggested in this blog post.
@Methuselah96 Thanks for your feedback!
- You're right. I had
getDerivedStateFromProps
initially in place but thought it would be redundant because of the check inshouldComponentUpdate
. I see it now I was clearly wrong. - I am aware of the article but didn't want to add another dependency. If it is ok for you, I'll refactor to use memoize-one.
@BrunoFenzl Yeah, I'm fine with adding memoize-one
as a dependency. Thanks!
Hi, can this PR be merged? Thanks.
The feedback I gave still needs to be addressed. Feel free to create your own PR since it seems like this PR might be abandoned.
@Methuselah96 Would you be open to a PR which removes these unsafe calls by converting the JSONTree and JSONNestedNode to function components and using hooks?
@defunctzombie Yeah, go for it!
Resolved by https://github.com/reduxjs/redux-devtools/pull/1288.