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

Make all readContext() and Hook-in-a-Hook checks DEV-only #14677

Merged
merged 6 commits into from Jan 24, 2019

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Jan 23, 2019

  • Changes it to be DEV-only
  • Implements it for SSR
  • Adds a similar DEV-only check for render phase class updates
  • Changes all Hooks-in-Hooks warnings to similarly be DEV-only, removing the flaky current variable reassignment that led to bugs (includes a regression test)

@sizebot
Copy link

sizebot commented Jan 23, 2019

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

Details of bundled changes.

Comparing: 6cb2677...70c68a2

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js +0.1% +0.2% 739.73 KB 740.45 KB 170.54 KB 170.84 KB UMD_DEV
react-dom.production.min.js -0.2% -0.1% 103.78 KB 103.55 KB 33.88 KB 33.84 KB UMD_PROD
react-dom.profiling.min.js -0.2% -0.3% 106.78 KB 106.55 KB 34.5 KB 34.4 KB UMD_PROFILING
react-dom.development.js +0.1% +0.2% 734.79 KB 735.51 KB 169.11 KB 169.42 KB NODE_DEV
react-dom.production.min.js -0.2% -0.2% 103.84 KB 103.61 KB 33.41 KB 33.35 KB NODE_PROD
react-dom.profiling.min.js -0.2% -0.2% 106.95 KB 106.72 KB 33.98 KB 33.93 KB NODE_PROFILING
ReactDOM-dev.js +0.1% +0.2% 757.29 KB 758.14 KB 170.34 KB 170.63 KB FB_WWW_DEV
ReactDOM-prod.js -0.5% -0.2% 312.5 KB 311.04 KB 57.56 KB 57.43 KB FB_WWW_PROD
ReactDOM-profiling.js -0.5% -0.3% 319.66 KB 318.21 KB 58.88 KB 58.73 KB FB_WWW_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.2% 740.07 KB 740.79 KB 170.67 KB 170.98 KB UMD_DEV
react-dom-unstable-fire.production.min.js -0.2% -0.1% 103.79 KB 103.57 KB 33.88 KB 33.84 KB UMD_PROD
react-dom-unstable-fire.profiling.min.js -0.2% -0.3% 106.8 KB 106.57 KB 34.5 KB 34.41 KB UMD_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.2% 735.13 KB 735.85 KB 169.25 KB 169.56 KB NODE_DEV
react-dom-unstable-fire.production.min.js -0.2% -0.2% 103.85 KB 103.62 KB 33.42 KB 33.36 KB NODE_PROD
react-dom-unstable-fire.profiling.min.js -0.2% -0.2% 106.97 KB 106.74 KB 33.99 KB 33.94 KB NODE_PROFILING
ReactFire-dev.js +0.1% +0.2% 756.5 KB 757.35 KB 170.26 KB 170.55 KB FB_WWW_DEV
ReactFire-prod.js -0.5% -0.3% 301.09 KB 299.64 KB 55.26 KB 55.11 KB FB_WWW_PROD
ReactFire-profiling.js -0.5% -0.2% 308.18 KB 306.73 KB 56.55 KB 56.43 KB FB_WWW_PROFILING
react-dom-test-utils.development.js 0.0% -0.0% 44.87 KB 44.87 KB 12.3 KB 12.3 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% -0.0% 9.74 KB 9.74 KB 3.65 KB 3.65 KB NODE_PROD
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.1% 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.1% 10.75 KB 10.75 KB 3.71 KB 3.71 KB NODE_PROD
react-dom-server.browser.development.js +0.5% +0.5% 124.9 KB 125.5 KB 33.32 KB 33.48 KB UMD_DEV
react-dom-server.browser.production.min.js -0.3% -0.3% 18.63 KB 18.57 KB 7.19 KB 7.16 KB UMD_PROD
react-dom-server.browser.development.js +0.5% +0.5% 121.03 KB 121.63 KB 32.39 KB 32.55 KB NODE_DEV
react-dom-server.browser.production.min.js -0.3% -0.4% 18.54 KB 18.49 KB 7.17 KB 7.15 KB NODE_PROD
ReactDOMServer-dev.js +0.6% +0.6% 121.85 KB 122.59 KB 31.87 KB 32.05 KB FB_WWW_DEV
ReactDOMServer-prod.js -1.0% -0.7% 45.18 KB 44.73 KB 10.39 KB 10.32 KB FB_WWW_PROD
react-dom-server.node.development.js +0.5% +0.5% 123.09 KB 123.69 KB 32.94 KB 33.09 KB NODE_DEV
react-dom-server.node.production.min.js -0.3% -0.4% 19.42 KB 19.36 KB 7.48 KB 7.45 KB NODE_PROD
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.1% 518.57 KB 519.29 KB 114.38 KB 114.44 KB UMD_DEV
react-art.production.min.js -0.2% -0.3% 95.84 KB 95.6 KB 29.73 KB 29.64 KB UMD_PROD
react-art.development.js +0.2% +0.1% 450.09 KB 450.82 KB 97.27 KB 97.34 KB NODE_DEV
react-art.production.min.js -0.4% -0.4% 60.86 KB 60.63 KB 18.89 KB 18.81 KB NODE_PROD
ReactART-dev.js +0.2% +0.3% 459.01 KB 459.86 KB 96.4 KB 96.66 KB FB_WWW_DEV
ReactART-prod.js -0.8% -0.4% 188.4 KB 186.94 KB 32.23 KB 32.09 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.0% 583.86 KB 584.71 KB 127.02 KB 127.06 KB RN_FB_DEV
ReactNativeRenderer-prod.js -0.6% -0.3% 244.14 KB 242.69 KB 42.9 KB 42.75 KB RN_FB_PROD
ReactNativeRenderer-profiling.js -0.6% -0.3% 250.29 KB 248.83 KB 44.23 KB 44.1 KB RN_FB_PROFILING
ReactNativeRenderer-dev.js +0.1% 0.0% 583.78 KB 584.62 KB 126.98 KB 127.03 KB RN_OSS_DEV
ReactNativeRenderer-prod.js -0.6% -0.3% 244.16 KB 242.7 KB 42.9 KB 42.75 KB RN_OSS_PROD
ReactNativeRenderer-profiling.js -0.6% -0.3% 250.31 KB 248.85 KB 44.23 KB 44.1 KB RN_OSS_PROFILING
ReactFabric-dev.js +0.1% 0.0% 574.72 KB 575.56 KB 124.75 KB 124.8 KB RN_FB_DEV
ReactFabric-prod.js -0.6% -0.3% 237.34 KB 235.89 KB 41.45 KB 41.31 KB RN_FB_PROD
ReactFabric-profiling.js -0.6% -0.4% 242.64 KB 241.19 KB 42.77 KB 42.61 KB RN_FB_PROFILING
ReactFabric-dev.js +0.1% 0.0% 574.62 KB 575.47 KB 124.71 KB 124.76 KB RN_OSS_DEV
ReactFabric-prod.js -0.6% -0.3% 237.35 KB 235.9 KB 41.44 KB 41.31 KB RN_OSS_PROD
ReactFabric-profiling.js -0.6% -0.4% 242.65 KB 241.2 KB 42.76 KB 42.61 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.2% +0.3% 463.31 KB 464.03 KB 100.02 KB 100.27 KB UMD_DEV
react-test-renderer.production.min.js -0.4% -0.6% 62.25 KB 62.02 KB 19.35 KB 19.25 KB UMD_PROD
react-test-renderer.development.js +0.2% +0.3% 458.27 KB 458.99 KB 98.79 KB 99.04 KB NODE_DEV
react-test-renderer.production.min.js -0.4% -0.4% 61.93 KB 61.7 KB 19.14 KB 19.07 KB NODE_PROD
ReactTestRenderer-dev.js +0.2% +0.3% 467.76 KB 468.6 KB 98.39 KB 98.66 KB FB_WWW_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.0% 11.05 KB 11.05 KB 3.34 KB 3.33 KB UMD_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.development.js +0.2% +0.1% 447.92 KB 448.65 KB 95.77 KB 95.84 KB NODE_DEV
react-reconciler.production.min.js -0.4% -0.3% 62.04 KB 61.81 KB 18.74 KB 18.69 KB NODE_PROD
react-reconciler-persistent.development.js +0.2% +0.1% 446.3 KB 447.03 KB 95.12 KB 95.2 KB NODE_DEV
react-reconciler-persistent.production.min.js -0.4% -0.3% 62.05 KB 61.82 KB 18.75 KB 18.7 KB NODE_PROD
react-reconciler-reflection.production.min.js 0.0% -0.1% 2.56 KB 2.56 KB 1.13 KB 1.13 KB NODE_PROD

Generated by 🚫 dangerJS

gaearon added a commit to gaearon/react that referenced this pull request Jan 23, 2019
Make readContext() inside Hooks check DEV-only
@gaearon gaearon mentioned this pull request Jan 23, 2019
@gaearon gaearon changed the title Make readContext() inside Hooks check DEV-only Make all readContext() and Hook-in-a-Hook checks DEV-only Jan 23, 2019
@gaearon
Copy link
Collaborator Author

gaearon commented Jan 23, 2019

Ready for review.

Copy link
Collaborator

@sebmarkbage sebmarkbage left a comment

Choose a reason for hiding this comment

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

So there is another technique we have for hooks. If you think about them as algebraic effects, how do you control the implementation within various contexts?

You change the current dispatcher.

I believe we did that in some other place.

If you wrote a custom dispatcher which wrapped useContext/readContext, in DEV, what would else be different on that dispatcher?

Would you also disallow other hooks in the same scope?

currentlyRenderingFiber = null;
lastContextDependency = null;
lastContextWithAllBitsObserved = null;
export function stashContextDependenciesInDEV(): void {
Copy link
Collaborator

Choose a reason for hiding this comment

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

These names don’t really make sense anymore.

How about enter/exitDisallowedContextRead?

@gaearon
Copy link
Collaborator Author

gaearon commented Jan 24, 2019

A separate dispatcher sounds like a cleaner way to do it. I initially thought of replacing the dispatcher while we're inside a Hook like useMemo but then decided against it. I didn't think about wrapping the whole thing in DEV. Do you want to explore it in this PR, or land it first and I can iterate after 16.8?

@gaearon
Copy link
Collaborator Author

gaearon commented Jan 24, 2019

I don't understand what you mean by "other hooks in the same scope".

@sebmarkbage
Copy link
Collaborator

I think the way we allow currentlyRenderingFiber to be null is also wrong. That should also be done with the dispatcher technique.

I have a hard time reviewing these because it’s so spread out. If we’re confident that doing this whole refactor after 16.8, it’s fine but I can’t tell if that’s a breaking change or not from the code.

Copy link
Collaborator

@sebmarkbage sebmarkbage left a comment

Choose a reason for hiding this comment

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

Your call/responsibility.

@sebmarkbage
Copy link
Collaborator

For the currentlyRenderingFiber thing. It should always be non-null when the dispatcher is set because we’re always rendering something. We should instead swap out the whole dispatcher when we’re inside user code.

That way we also remove the whole resolveRenderingThingy() call in each hook.

@sebmarkbage
Copy link
Collaborator

By “Other hooks in the same scope” I mean, it’s not clear to me from the code which hooks are disallowed and in what user code. Is it that most user code is covered by the “currentlyRenderingFiber === null” check and this is adding something additional or are there scopes where either one is in effect but not both?

@gaearon
Copy link
Collaborator Author

gaearon commented Jan 24, 2019

I think there's some confusion here.

Here's a brief history of this code:

  • Initially, currentlyRenderingFiber === null only happened in classes (and in general, when we're not rendering Hooks). It was never null while rendering Hook-capable components.

  • Throw an error when using hooks inside useMemo, or .memo's comparison function #14608 repurposed currentlyRenderingFiber === null check to also mean "inside user code".

  • This PR backs out of that change, reverting currentlyRenderingFiber === null to its original meaning ("we're not rendering a function component"). But it adds DEV warnings with a separate flag for user code.

@gaearon
Copy link
Collaborator Author

gaearon commented Jan 24, 2019

By “Other hooks in the same scope” I mean, it’s not clear to me from the code which hooks are disallowed and in what user code. Is it that most user code is covered by the “currentlyRenderingFiber === null” check and this is adding something additional or are there scopes where either one is in effect but not both?

Before #14608

  • currentlyRenderingFiber === null only if we're not currently rendering a function component.
  • If you call a Hook inside a Hook (e.g. useMemo from useReducer), nothing tells you it was wrong.

Between #14608 and This PR

  • currentlyRenderingFiber === null if we're not currently rendering a function component or if we entered user code from a Hook (e.g. either fn in useMemo(fn), fn in useReducer(fn), fn in useState(fn))
  • If you call a Hook inside a Hook (e.g. useMemo from useReducer), you get an invariant.

This PR

  • currentlyRenderingFiber === null only if we're not currently rendering a function component.
  • If you call a Hook inside a Hook (e.g. useMemo from useReducer), you get a warning.

@gaearon gaearon merged commit 8bcc88f into facebook:master Jan 24, 2019
@gaearon gaearon deleted the readcontext-warning branch January 24, 2019 19:31
n8schloss pushed a commit to n8schloss/react that referenced this pull request Jan 31, 2019
…4677)

* Make readContext() in Hooks DEV-only warning

* Warn about readContext() during class render-phase setState()

* Warn on readContext() in SSR inside useMemo and useReducer

* Make all Hooks-in-Hooks warnings DEV-only

* Rename stashContextDependencies

* Clean up warning state on errors
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