Skip to content
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 2 commits into from
Jan 21, 2019
Merged

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Jan 21, 2019

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 to FiberHooks implementation so that we can check currentlyRenderingFiber (which #14608 temporarily resets before calling user code from Hooks). But then I realized currentlyRenderingFiber is also null for classes and context consumers from which it's legit to readContext().

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 if renderExpirationTime !== NoWork (we’re using Hooks) and currentlyRenderingFiber === 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, as FiberBeginWork 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.

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.
// 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

til, good check.

@sizebot
Copy link

sizebot commented Jan 21, 2019

ReactDOM: size: -0.1%, gzip: -0.1%

Details of bundled changes.

Comparing: 2a084f5...88b58dc

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js +0.1% +0.1% 734.27 KB 734.82 KB 169.2 KB 169.39 KB UMD_DEV
react-dom.production.min.js -0.1% -0.1% 98.98 KB 98.9 KB 32.2 KB 32.18 KB UMD_PROD
react-dom.profiling.min.js -0.1% -0.0% 101.97 KB 101.89 KB 32.84 KB 32.83 KB UMD_PROFILING
react-dom.development.js +0.1% +0.1% 729.33 KB 729.88 KB 167.77 KB 167.99 KB NODE_DEV
react-dom.production.min.js -0.1% -0.0% 98.99 KB 98.91 KB 31.68 KB 31.68 KB NODE_PROD
react-dom.profiling.min.js -0.1% -0.0% 102.09 KB 102.01 KB 32.31 KB 32.29 KB NODE_PROFILING
ReactDOM-dev.js +0.1% +0.1% 751.7 KB 752.27 KB 169.05 KB 169.27 KB FB_WWW_DEV
ReactDOM-prod.js -0.0% -0.1% 311.52 KB 311.45 KB 57.5 KB 57.46 KB FB_WWW_PROD
ReactDOM-profiling.js -0.0% -0.1% 318.68 KB 318.61 KB 58.83 KB 58.79 KB FB_WWW_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.1% 734.61 KB 735.16 KB 169.34 KB 169.53 KB UMD_DEV
react-dom-unstable-fire.production.min.js -0.1% -0.1% 99 KB 98.92 KB 32.21 KB 32.19 KB UMD_PROD
react-dom-unstable-fire.profiling.min.js -0.1% -0.0% 101.98 KB 101.9 KB 32.85 KB 32.84 KB UMD_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.1% 729.67 KB 730.22 KB 167.92 KB 168.14 KB NODE_DEV
react-dom-unstable-fire.production.min.js -0.1% -0.0% 99 KB 98.92 KB 31.69 KB 31.69 KB NODE_PROD
react-dom-unstable-fire.profiling.min.js -0.1% -0.0% 102.1 KB 102.02 KB 32.32 KB 32.3 KB NODE_PROFILING
ReactFire-dev.js +0.1% +0.1% 750.91 KB 751.48 KB 169 KB 169.22 KB FB_WWW_DEV
ReactFire-prod.js -0.0% -0.1% 300.11 KB 300.04 KB 55.23 KB 55.18 KB FB_WWW_PROD
ReactFire-profiling.js -0.0% -0.1% 307.2 KB 307.13 KB 56.51 KB 56.46 KB FB_WWW_PROFILING
react-dom-test-utils.production.min.js 0.0% 0.0% 9.97 KB 9.97 KB 3.71 KB 3.71 KB UMD_PROD
react-dom-test-utils.production.min.js 0.0% 🔺+0.1% 9.74 KB 9.74 KB 3.65 KB 3.65 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% 0.0% 60.61 KB 60.61 KB 15.92 KB 15.92 KB UMD_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% 0.0% 11.01 KB 11.01 KB 3.81 KB 3.81 KB UMD_PROD
react-dom-unstable-native-dependencies.development.js 0.0% 0.0% 60.29 KB 60.29 KB 15.79 KB 15.79 KB NODE_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% 0.0% 10.75 KB 10.75 KB 3.71 KB 3.71 KB NODE_PROD
react-dom-server.browser.development.js 0.0% 0.0% 125.09 KB 125.09 KB 33.35 KB 33.35 KB UMD_DEV
react-dom-server.browser.development.js 0.0% 0.0% 121.22 KB 121.22 KB 32.43 KB 32.43 KB NODE_DEV
ReactDOMServer-dev.js 0.0% 0.0% 122.4 KB 122.4 KB 32.07 KB 32.07 KB FB_WWW_DEV
ReactDOMServer-prod.js 0.0% 0.0% 45.18 KB 45.18 KB 10.39 KB 10.4 KB FB_WWW_PROD
react-dom-server.node.development.js 0.0% 0.0% 123.28 KB 123.28 KB 32.97 KB 32.97 KB NODE_DEV
react-dom-server.node.production.min.js 0.0% 0.0% 17.74 KB 17.74 KB 6.83 KB 6.83 KB NODE_PROD
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.63 KB 3.63 KB 1.44 KB 1.44 KB UMD_DEV
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.45 KB 3.45 KB 1.39 KB 1.39 KB NODE_DEV

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +0.1% +0.2% 513.11 KB 513.66 KB 113.03 KB 113.22 KB UMD_DEV
react-art.production.min.js -0.1% 0.0% 91.04 KB 90.96 KB 27.97 KB 27.97 KB UMD_PROD
react-art.development.js +0.1% +0.2% 444.63 KB 445.19 KB 95.92 KB 96.15 KB NODE_DEV
react-art.production.min.js -0.1% -0.1% 56.01 KB 55.94 KB 17.25 KB 17.23 KB NODE_PROD
ReactART-dev.js +0.1% +0.2% 453.42 KB 453.99 KB 95.09 KB 95.31 KB FB_WWW_DEV
ReactART-prod.js -0.0% -0.2% 187.42 KB 187.35 KB 32.15 KB 32.1 KB FB_WWW_PROD

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactNativeRenderer-dev.js +0.1% +0.2% 577.89 KB 578.49 KB 125.6 KB 125.82 KB RN_FB_DEV
ReactNativeRenderer-prod.js 0.0% 0.0% 243.02 KB 243.14 KB 42.77 KB 42.79 KB RN_FB_PROD
ReactNativeRenderer-profiling.js 0.0% 0.0% 249.17 KB 249.28 KB 44.12 KB 44.13 KB RN_FB_PROFILING
ReactNativeRenderer-dev.js +0.1% +0.2% 577.8 KB 578.41 KB 125.56 KB 125.78 KB RN_OSS_DEV
ReactNativeRenderer-prod.js -0.1% -0.1% 227.3 KB 227.12 KB 39.53 KB 39.48 KB RN_OSS_PROD
ReactNativeRenderer-profiling.js -0.1% -0.1% 233.31 KB 233.13 KB 40.91 KB 40.86 KB RN_OSS_PROFILING
ReactFabric-dev.js +0.1% +0.2% 568.74 KB 569.34 KB 123.32 KB 123.54 KB RN_FB_DEV
ReactFabric-prod.js 0.0% 0.0% 236.22 KB 236.34 KB 41.29 KB 41.31 KB RN_FB_PROD
ReactFabric-profiling.js 0.0% 0.0% 241.52 KB 241.63 KB 42.64 KB 42.66 KB RN_FB_PROFILING
ReactFabric-dev.js +0.1% +0.2% 568.65 KB 569.25 KB 123.27 KB 123.5 KB RN_OSS_DEV
ReactFabric-prod.js -0.1% -0.1% 220.53 KB 220.35 KB 38.07 KB 38.02 KB RN_OSS_PROD
ReactFabric-profiling.js -0.1% -0.1% 225.62 KB 225.44 KB 39.45 KB 39.4 KB RN_OSS_PROFILING

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +0.1% +0.2% 457.85 KB 458.4 KB 98.68 KB 98.86 KB UMD_DEV
react-test-renderer.production.min.js -0.1% -0.1% 57.41 KB 57.33 KB 17.62 KB 17.61 KB UMD_PROD
react-test-renderer.development.js +0.1% +0.2% 452.81 KB 453.36 KB 97.45 KB 97.63 KB NODE_DEV
react-test-renderer.production.min.js -0.1% -0.1% 57.08 KB 57 KB 17.47 KB 17.45 KB NODE_PROD
ReactTestRenderer-dev.js +0.1% +0.2% 461.81 KB 462.38 KB 96.97 KB 97.19 KB FB_WWW_DEV
react-test-renderer-shallow.development.js 0.0% 0.0% 32.39 KB 32.39 KB 8.46 KB 8.46 KB NODE_DEV
react-test-renderer-shallow.production.min.js 0.0% 0.0% 11.2 KB 11.2 KB 3.49 KB 3.49 KB NODE_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.development.js +0.1% +0.2% 442.46 KB 443.02 KB 94.43 KB 94.61 KB NODE_DEV
react-reconciler.production.min.js -0.1% -0.0% 57.17 KB 57.09 KB 17.07 KB 17.07 KB NODE_PROD
react-reconciler-persistent.development.js +0.1% +0.2% 440.84 KB 441.39 KB 93.79 KB 93.96 KB NODE_DEV
react-reconciler-persistent.production.min.js -0.1% -0.0% 57.18 KB 57.1 KB 17.08 KB 17.08 KB NODE_PROD
react-reconciler-reflection.development.js 0.0% 0.0% 15.4 KB 15.4 KB 4.83 KB 4.84 KB NODE_DEV

Generated by 🚫 dangerJS

@gaearon gaearon merged commit 1fcbd22 into facebook:master Jan 21, 2019
@gaearon gaearon deleted the ctx-bad branch January 21, 2019 19:06
@gaearon gaearon restored the ctx-bad branch January 21, 2019 20:28
gaearon added a commit that referenced this pull request Jan 21, 2019
gaearon added a commit that referenced this pull request Jan 21, 2019
* 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.
gaearon added a commit that referenced this pull request Jan 21, 2019
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants