-
Notifications
You must be signed in to change notification settings - Fork 562
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
async-safe, static lifecycle hooks #6
Merged
+442
−0
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9818f57
Initial proposal for async-friendly, static lifecycle hooks
bvaughn 839547f
Added a note about the prefix
bvaughn b7189cc
Made deprecation/rename clearer
bvaughn aad6845
Hopefully clarified the computeMemoizeData() placeholder example
bvaughn 99988da
Renamed example method for improved clarity
bvaughn 4a34d25
Added note about aEL being unsafe in cWM
bvaughn 902d4b3
Fixed typo
bvaughn 3df7ce6
Fixing typo
bvaughn 1864c93
Removed Facebook-specific terminology
bvaughn 428758b
Tweaked wording for clarity
bvaughn cfcb7e8
Reorganization (part 1, more to come)
bvaughn 536084d
Added some more focused examples
bvaughn a1431a4
Added a comment about calling setState on a possibly unmounted component
bvaughn 3c6132e
Cancel async request on unmount in example
bvaughn 4425dbe
Tweaking examples based on Dan's feedback
bvaughn 67272ce
Renamed deriveStateFromProps to getDerivedStateFromNextProps
bvaughn c7f6728
Renamed prefetch to optimisticallyPrepareToRender
bvaughn bb2d246
Added `render` to a list
bvaughn 8618f70
Removed static optimisticallyPrepareToRender() in favor of render()
bvaughn 8f6c20e
Renamed getDerivedStateFromNextProps to getDerivedStateFromProps
bvaughn e05e317
Updated when getDerivedStateFromProps is called and what its argument…
bvaughn 7042a2a
Renamed unsafe_* to UNSAFE_*
bvaughn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Renamed getDerivedStateFromNextProps to getDerivedStateFromProps
commit 8f6c20edd98f16ecc7395b83332944d1757590a4
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the benefit of this API design?
What about
undefined
something like this?I prefer to return
prevState
explicitly if we don't need to update the state.Return
null
to indicate no change to state might be unclear for developers.Also, waring for returning
null
orundefined
is good for me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It signals a clear intent. Undefined return value may indicate someone forget a default case or an else statement, etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bvaughn Thank you for your response!
What happened if
getDerivedStateFromNextProps
returnsundefined
? It is ignored and the state won't be changed?In that case, Return
prevState
seems to be clearer than returnnull
.Why is
null
treated as a special case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem. 😄
Probably log a warning, suggesting the user return
null
instead, to avoid the potential accidental omission of a return statement like I mentioned before.I think
null
is safer. We could compare the identity of the returned value toprevState
, but what if a user modified values onprevState
(same object, but mutated keys - although to be clear, this should not be done).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
👍
I got it.
But why is comparing the identity of the returned value to prevState needed?
Will the result be used for something?
Are there any problems to use a value returned from
getDerivedStateFromNextProps
without the comparing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the returned value, like the value passed to
setState
, can be a partial state- meaning that we need to (eventually) merge it into the current state. Merging the current state with itself is doing work unnecessarily- and a subsequent call torender()
(the default behavior whenstate
is updated) is more work- so it's nice for us to have a faster way to determine there's no actual work to be done.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks That makes sense!I missed the return type is
PartialState | null
.setState(updater)
can acceptundefined
to indicate no change not onlynull
so it might also make sense to acceptundefined
as well as a perspective of API consistency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not an unreasonable suggestion, but I disagree with it. To me, it seems more likely that one might forget to handle a return case than it is for one to accidentally call
this.setState
with an undefined value. React's DEV warnings are there to protect users from accidental oversights, and this (in my mind) is more similar to forgetting to return a value fromrender
(which we also don't support).