-
Notifications
You must be signed in to change notification settings - Fork 570
async-safe, static lifecycle hooks #6
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
Merged
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
Hopefully clarified the computeMemoizeData() placeholder example
- Loading branch information
commit aad6845560c562b23de416dd37b4f34aa0bf2472
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ class ExampleComponent extends React.Component { | |
|
|
||
| addExternalEventListeners(); | ||
|
|
||
| computeMemoizeData(nextProps, nextState); | ||
| this._computeMemoizeData(nextProps); | ||
|
||
| } | ||
|
|
||
| componentWillReceiveProps(nextProps) { | ||
|
|
@@ -57,7 +57,7 @@ class ExampleComponent extends React.Component { | |
| nextProps.onChange(nextState.someStatefulValue); | ||
| } | ||
|
|
||
| computeMemoizeData(nextProps, nextState); | ||
| this._computeMemoizeData(nextProps); | ||
| } | ||
|
|
||
| render() { | ||
|
|
@@ -127,8 +127,10 @@ class ExampleComponent extends React.Component { | |
|
|
||
| render() { | ||
| // Memoization that doesn't go in state can be done in render. | ||
| // It should be idempotent and have no external side effects though. | ||
| computeMemoizeData(this.props, this.state); | ||
| // It should be idempotent and have no external side effects or mutations. | ||
| // Examples include incrementing unique ids, | ||
| // Lazily calculating and caching values, etc. | ||
| this._computeMemoizeData(this.props); | ||
|
|
||
| if (this.state.externalData === null) { | ||
| return <div>Loading...</div>; | ||
|
|
||
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.
Adding event listeners in componentWillMount is a bug, right? I know that it would make SSR fail (since there's no DOM), and it seems like you say it's a problem in the Common Problems section. If that's right, a comment to that effect would be super helpful!
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.
Flux stores are external event listeners that don't need a DOM, but also SSR alone is not necessarily that common. Especially for long tail.
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's helpful context, but my question here is whether or not this line is currently considered a bug. It sounds like this document is saying yes, it is, although I'm not entirely sure. If so, I think a comment would help understanding. Does that make sense?
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.
Yeah. This is not a good pattern. (I listed it below as a common problem we see.) I show it here only to show where it should be done instead. I've added an inline comment to this part of the example though to make it clear that it's not a pattern we recommend. 😄