Skip to content

Add a feature flag to disable legacy context - #16269

Merged
gaearon merged 7 commits into
react:masterfrom
gaearon:ctx
Aug 2, 2019
Merged

Add a feature flag to disable legacy context#16269
gaearon merged 7 commits into
react:masterfrom
gaearon:ctx

Conversation

@gaearon

@gaearongaearon commented Jul 31, 2019

Copy link
Copy Markdown
Collaborator

Adds a feature flag that disables the legacy context API. The feature flag is turned off in open source builds. Strict Mode is already a mechanism that warns about it, but this feature flag actually makes this.context always an empty object, and context argument to functions always undefined. So it's a way to enforce it. If we later offer "modern" builds, we can turn it on there by default. For now, I want to use this internally for newly written code.

No whitespace: https://github.com/facebook/react/compare/master...gaearon:ctx?expand=1&w=1

@sizebot

sizebot commented Jul 31, 2019

Copy link
Copy Markdown

ReactDOM: size: 0.0%, gzip: 🔺+0.1%

Details of bundled changes.

Comparing: 4279455...892df46

react-dom

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
react-dom.profiling.min.js0.0%-0.0%115.03 KB115.03 KB36.19 KB36.19 KBNODE_PROFILING
react-dom-server.browser.development.js+1.1%+0.6%137.04 KB138.51 KB36.22 KB36.43 KBUMD_DEV
ReactDOM-dev.js+0.3%+0.2%929.88 KB932.35 KB205.77 KB206.09 KBFB_WWW_DEV
react-dom-server.browser.production.min.js0.0%🔺+0.1%19.39 KB19.39 KB7.27 KB7.27 KBUMD_PROD
react-dom-test-utils.development.js0.0%-0.0%55.72 KB55.72 KB14.95 KB14.95 KBUMD_DEV
react-dom-unstable-fizz.browser.development.js0.0%-0.1%3.78 KB3.78 KB1.52 KB1.52 KBUMD_DEV
react-dom-test-utils.production.min.js0.0%🔺+0.1%11.19 KB11.19 KB4.11 KB4.11 KBUMD_PROD
react-dom-unstable-fizz.browser.production.min.js0.0%-0.3%1.21 KB1.21 KB708 B706 BUMD_PROD
react-dom-test-utils.development.js0.0%-0.0%53.99 KB53.99 KB14.62 KB14.62 KBNODE_DEV
react-dom-unstable-fizz.browser.development.js0.0%-0.1%3.61 KB3.61 KB1.48 KB1.48 KBNODE_DEV
react-dom-test-utils.production.min.js0.0%0.0%10.97 KB10.97 KB4.04 KB4.04 KBNODE_PROD
react-dom-unstable-fizz.browser.production.min.js0.0%-0.2%1.05 KB1.05 KB638 B637 BNODE_PROD
react-dom.development.js+0.2%+0.1%906.48 KB908.65 KB205.72 KB205.96 KBUMD_DEV
react-dom.production.min.js0.0%-0.0%111.4 KB111.4 KB35.79 KB35.78 KBUMD_PROD
react-dom.profiling.min.js0.0%-0.0%114.81 KB114.81 KB36.83 KB36.83 KBUMD_PROFILING
react-dom.development.js+0.2%+0.1%900.78 KB902.95 KB204.12 KB204.37 KBNODE_DEV
react-dom-server.node.development.js+1.1%+0.6%135.1 KB136.57 KB35.83 KB36.04 KBNODE_DEV
react-dom-server.node.production.min.js0.0%🔺+0.1%20.17 KB20.17 KB7.57 KB7.57 KBNODE_PROD
ReactDOM-prod.js🔺+0.3%🔺+0.3%370.55 KB371.62 KB68.12 KB68.3 KBFB_WWW_PROD
ReactDOM-profiling.js+0.3%+0.3%375.2 KB376.27 KB69.13 KB69.32 KBFB_WWW_PROFILING
react-dom-server.browser.development.js+1.1%+0.6%133.17 KB134.64 KB35.29 KB35.5 KBNODE_DEV
react-dom-server.browser.production.min.js0.0%0.0%19.32 KB19.32 KB7.26 KB7.27 KBNODE_PROD
react-dom-unstable-native-dependencies.development.js0.0%-0.0%60.76 KB60.76 KB15.85 KB15.85 KBUMD_DEV
ReactDOMServer-dev.js+1.3%+0.6%135.2 KB136.95 KB34.68 KB34.9 KBFB_WWW_DEV
ReactDOMServer-prod.js🔺+0.6%🔺+0.8%46.38 KB46.68 KB10.75 KB10.83 KBFB_WWW_PROD
react-dom-unstable-native-dependencies.development.js0.0%-0.0%60.43 KB60.43 KB15.72 KB15.72 KBNODE_DEV
react-dom-unstable-fizz.node.development.js0.0%-0.1%3.85 KB3.85 KB1.5 KB1.5 KBNODE_DEV
react-dom-unstable-fizz.node.production.min.js0.0%-0.3%1.1 KB1.1 KB669 B667 BNODE_PROD

react-art

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
react-art.development.js+0.3%+0.3%643.83 KB645.99 KB140.56 KB140.93 KBUMD_DEV
react-art.production.min.js0.0%-0.0%102.3 KB102.3 KB31.21 KB31.21 KBUMD_PROD
react-art.development.js+0.4%+0.2%574.7 KB576.87 KB123.25 KB123.5 KBNODE_DEV
react-art.production.min.js0.0%-0.0%67.34 KB67.34 KB20.53 KB20.53 KBNODE_PROD
ReactART-dev.js+0.4%+0.2%588.65 KB591.12 KB122.83 KB123.13 KBFB_WWW_DEV
ReactART-prod.js🔺+0.5%🔺+0.5%219.98 KB221.04 KB37.5 KB37.7 KBFB_WWW_PROD

react-test-renderer

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
ReactTestRenderer-dev.js+0.4%+0.2%599.62 KB602.07 KB125.29 KB125.57 KBFB_WWW_DEV
react-test-renderer-shallow.development.js0.0%-0.0%39.51 KB39.51 KB9.95 KB9.95 KBUMD_DEV
react-test-renderer-shallow.production.min.js0.0%-0.0%11.66 KB11.66 KB3.56 KB3.56 KBUMD_PROD
react-test-renderer-shallow.development.js0.0%-0.0%33.64 KB33.64 KB8.54 KB8.53 KBNODE_DEV
react-test-renderer-shallow.production.min.js0.0%-0.0%11.81 KB11.81 KB3.69 KB3.69 KBNODE_PROD
react-test-renderer.development.js+0.4%+0.2%587.61 KB589.77 KB125.85 KB126.09 KBUMD_DEV
react-test-renderer.production.min.js0.0%-0.0%68.85 KB68.85 KB21.05 KB21.05 KBUMD_PROD
react-test-renderer.development.js+0.4%+0.2%583.15 KB585.31 KB124.73 KB125 KBNODE_DEV
react-test-renderer.production.min.js0.0%-0.0%68.59 KB68.59 KB20.85 KB20.85 KBNODE_PROD

react-reconciler

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
react-reconciler.development.js+0.4%+0.2%572.79 KB574.96 KB121.81 KB122.07 KBNODE_DEV
react-reconciler-reflection.development.js0.0%-0.0%16.63 KB16.63 KB5.16 KB5.16 KBNODE_DEV
react-reconciler-reflection.production.min.js0.0%-0.1%2.59 KB2.59 KB1.14 KB1.14 KBNODE_PROD
react-reconciler-persistent.development.js+0.4%+0.2%569.8 KB571.97 KB120.55 KB120.82 KBNODE_DEV
react-reconciler-persistent.production.min.js0.0%-0.0%68.58 KB68.58 KB20.38 KB20.38 KBNODE_PROD

react-native-renderer

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
ReactNativeRenderer-prod.js-0.0%-0.0%271.97 KB271.94 KB46.65 KB46.65 KBRN_OSS_PROD
ReactNativeRenderer-profiling.js-0.0%-0.0%280.39 KB280.36 KB48.19 KB48.18 KBRN_OSS_PROFILING
ReactFabric-prod.js-0.0%-0.0%265.65 KB265.62 KB45.7 KB45.69 KBRN_OSS_PROD
ReactFabric-profiling.js-0.0%-0.0%274.31 KB274.28 KB47.16 KB47.15 KBRN_OSS_PROFILING
ReactFabric-dev.js+0.3%+0.2%733.51 KB735.94 KB155.19 KB155.48 KBRN_FB_DEV
ReactFabric-prod.js-0.0%-0.0%265.65 KB265.62 KB45.71 KB45.7 KBRN_FB_PROD
ReactNativeRenderer-dev.js+0.3%+0.2%722.67 KB725.1 KB153.1 KB153.4 KBRN_OSS_DEV
ReactFabric-profiling.js-0.0%-0.0%274.31 KB274.28 KB47.16 KB47.16 KBRN_FB_PROFILING
ReactNativeRenderer-dev.js+0.3%+0.2%722.76 KB725.2 KB153.15 KB153.45 KBRN_FB_DEV
ReactNativeRenderer-prod.js-0.0%-0.0%271.96 KB271.93 KB46.66 KB46.66 KBRN_FB_PROD
ReactNativeRenderer-profiling.js-0.0%-0.0%280.38 KB280.35 KB48.19 KB48.18 KBRN_FB_PROFILING
ReactFabric-dev.js+0.3%+0.2%733.41 KB735.84 KB155.14 KB155.44 KBRN_OSS_DEV

Generated by 🚫 dangerJS

@acdliteacdlite left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What does this.context point to when the flag is on? Or the second argument to function components?

I think they should be undefined, instead of an empty object — i.e. it should behave the same as if legacy context never existed, no vestiges.

instance.__reactInternalMemoizedUnmaskedChildContext = unmaskedContext;
instance.__reactInternalMemoizedMaskedChildContext = maskedContext;
if (disableLegacyContext) {
invariant(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we make these dev-only warnings instead? Once everyone removes legacy context from their apps, we can remove the warning in a minor, whereas removing an invariant is an according-to-semver-pedants breaking change.

@gaearon

Copy link
Copy Markdown
CollaboratorAuthor

Previously, classes that weren't context consumers would get {}. You're saying it should be undefined too. That's a bigger breaking change but maybe it makes sense because it lets you find code with bad assumptions more easily.

- invariant -> warning
- Make this.context and context argument actually undefined
@acdlite

Copy link
Copy Markdown
Collaborator

That's a bigger breaking change but maybe it makes sense because it lets you find code with bad assumptions more easily.

Right, and then we don't have to do a second breaking change later

@gaearon

gaearon commented Aug 1, 2019

Copy link
Copy Markdown
CollaboratorAuthor

We could still remove the feature but keep it forever {}. Since it doesn't cost us anything to keep setting it once.

@acdlite

Copy link
Copy Markdown
Collaborator

@gaearon It's the second argument to functions that I care more about, personally. We might want to use that for something else.

Don't really care about classes. What's a class? 🤷‍♀️

@gaearon
gaearon merged commit 0c1ec04 into react:masterAug 2, 2019
@gaearon
gaearon deleted the ctx branch August 2, 2019 00:26
mrizwanashiq pushed a commit to mrizwanashiq/react that referenced this pull request Jun 25, 2026
* Add a feature flag to disable legacy context
* Address review
- invariant -> warning
- Make this.context and context argument actually undefined
* Increase test coverage for lifecycles
* Also disable it on the server is flag is on
* Make this.context {} when disabled, but function context is undefined
* Move checks inside
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@gaearon@sizebot@acdlite@facebook-github-bot