-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Disallow reading context during useMemo etc #14648
Merged
Merged
Conversation
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
The previous commit check was too broad and incorrectly restricted classes from calling readContext(). This check is more precise and only targets components that are Hook-capable. It exploits the fact that `renderExpirationTime` is never `NoWork` after `renderWithHooks` -- something we already rely on.
threepointone
approved these changes
Jan 21, 2019
// The inner check tells us we're currently in renderWithHooks() phase | ||
// rather than, for example, in a class or a context consumer. | ||
// Then we know it should be an error. | ||
if (renderExpirationTime !== NoWork) { |
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.
til, good check.
ReactDOM: size: -0.1%, gzip: -0.1% Details of bundled changes.Comparing: 2a084f5...88b58dc react-dom
react-art
react-native-renderer
react-test-renderer
react-reconciler
Generated by 🚫 dangerJS |
gaearon
added a commit
that referenced
this pull request
Jan 21, 2019
…Mode" (#14652) * Revert "Revert "Disallow reading context during useMemo etc" (#14651)" This reverts commit 5fce648. * Revert "Add test coverage for readContext() on the server (#14649)" This reverts commit fe2ecd2. * Revert "Warn about incorrect use of useImperativeHandle() (#14647)" This reverts commit 8f45a7f. * Revert "Disallow reading context during useMemo etc (#14648)" This reverts commit 1fcbd22. * Revert "Warn about refs on lazy function components (#14645)" This reverts commit 2a084f5. * Revert "Fix typo (#14560)" This reverts commit b5a3df6. * Revert "fix typo (#14316)" This reverts commit 9c146e6. * Revert "Mention forwardRef() in <Fn ref={...} /> errors and warnings (#14644)" This reverts commit baa6d40. * Revert "Double-render function components with Hooks in DEV in StrictMode (#14643)" This reverts commit a1414e8.
jetoneza
pushed a commit
to jetoneza/react
that referenced
this pull request
Jan 23, 2019
* Disallow reading context during useMemo etc * Continue allowing readContext() in classes and context consumers The previous commit check was too broad and incorrectly restricted classes from calling readContext(). This check is more precise and only targets components that are Hook-capable. It exploits the fact that `renderExpirationTime` is never `NoWork` after `renderWithHooks` -- something we already rely on.
jetoneza
pushed a commit
to jetoneza/react
that referenced
this pull request
Jan 23, 2019
* Revert "Add test coverage for readContext() on the server (facebook#14649)" This reverts commit fe2ecd2. * Revert "Warn about incorrect use of useImperativeHandle() (facebook#14647)" This reverts commit 8f45a7f. * Revert "Disallow reading context during useMemo etc (facebook#14648)" This reverts commit 1fcbd22.
jetoneza
pushed a commit
to jetoneza/react
that referenced
this pull request
Jan 23, 2019
…Mode" (facebook#14652) * Revert "Revert "Disallow reading context during useMemo etc" (facebook#14651)" This reverts commit 5fce648. * Revert "Add test coverage for readContext() on the server (facebook#14649)" This reverts commit fe2ecd2. * Revert "Warn about incorrect use of useImperativeHandle() (facebook#14647)" This reverts commit 8f45a7f. * Revert "Disallow reading context during useMemo etc (facebook#14648)" This reverts commit 1fcbd22. * Revert "Warn about refs on lazy function components (facebook#14645)" This reverts commit 2a084f5. * Revert "Fix typo (facebook#14560)" This reverts commit b5a3df6. * Revert "fix typo (facebook#14316)" This reverts commit 9c146e6. * Revert "Mention forwardRef() in <Fn ref={...} /> errors and warnings (facebook#14644)" This reverts commit baa6d40. * Revert "Double-render function components with Hooks in DEV in StrictMode (facebook#14643)" This reverts commit a1414e8.
n8schloss
pushed a commit
to n8schloss/react
that referenced
this pull request
Jan 31, 2019
* Disallow reading context during useMemo etc * Continue allowing readContext() in classes and context consumers The previous commit check was too broad and incorrectly restricted classes from calling readContext(). This check is more precise and only targets components that are Hook-capable. It exploits the fact that `renderExpirationTime` is never `NoWork` after `renderWithHooks` -- something we already rely on.
n8schloss
pushed a commit
to n8schloss/react
that referenced
this pull request
Jan 31, 2019
* Revert "Add test coverage for readContext() on the server (facebook#14649)" This reverts commit fe2ecd2. * Revert "Warn about incorrect use of useImperativeHandle() (facebook#14647)" This reverts commit 8f45a7f. * Revert "Disallow reading context during useMemo etc (facebook#14648)" This reverts commit 1fcbd22.
n8schloss
pushed a commit
to n8schloss/react
that referenced
this pull request
Jan 31, 2019
…Mode" (facebook#14652) * Revert "Revert "Disallow reading context during useMemo etc" (facebook#14651)" This reverts commit 5fce648. * Revert "Add test coverage for readContext() on the server (facebook#14649)" This reverts commit fe2ecd2. * Revert "Warn about incorrect use of useImperativeHandle() (facebook#14647)" This reverts commit 8f45a7f. * Revert "Disallow reading context during useMemo etc (facebook#14648)" This reverts commit 1fcbd22. * Revert "Warn about refs on lazy function components (facebook#14645)" This reverts commit 2a084f5. * Revert "Fix typo (facebook#14560)" This reverts commit b5a3df6. * Revert "fix typo (facebook#14316)" This reverts commit 9c146e6. * Revert "Mention forwardRef() in <Fn ref={...} /> errors and warnings (facebook#14644)" This reverts commit baa6d40. * Revert "Double-render function components with Hooks in DEV in StrictMode (facebook#14643)" This reverts commit a1414e8.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This extends #14608 to direct
readContext
calls and adds tests. It turned out it's not super easy.I started by directing Dispatcher's
readContext
toFiberHooks
implementation so that we can checkcurrentlyRenderingFiber
(which #14608 temporarily resets before calling user code from Hooks). But then I realizedcurrentlyRenderingFiber
is alsonull
for classes and context consumers from which it's legit toreadContext()
.I struggled a bit but then noticed we already rely on
renderExpirationTime
(per comment) to tell whether we're in a Hook-capable component or not. So I used that too, and only throw ifrenderExpirationTime !== NoWork
(we’re using Hooks) andcurrentlyRenderingFiber === null
(we’ve stepped into the user code).While I was there, I also removed unnecessary
FiberBeginWork -> CurrentDispatcher -> readContext
indirection in favor of a direct call, asFiberBeginWork
itself is our own code.This PR is client-side only. I started looking into SSR but I think there might be more warts there and I want to do it separately.