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

Improve warning for invalid class contextType #15142

Merged
merged 3 commits into from Mar 19, 2019

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Mar 18, 2019

This adds extra warning for when contextType exists but is undefined. This can happen due to module cycles in legacy environments (see #13969) and can be very difficult to debug.

SSR and regular version are copy pasted.

I separated the DEV check outside the actual code because unlike in the prod code, we actually do want to warn for undefined-but-not-truthy code path.

@sizebot
Copy link

sizebot commented Mar 18, 2019

ReactDOM: size: 0.0%, gzip: -0.0%

Details of bundled changes.

Comparing: df7b87d...a123591

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js +0.1% +0.1% 781.07 KB 781.98 KB 178.22 KB 178.44 KB UMD_DEV
react-dom.production.min.js 0.0% -0.0% 105.24 KB 105.24 KB 34.12 KB 34.12 KB UMD_PROD
react-dom.profiling.min.js 0.0% -0.0% 108.24 KB 108.24 KB 34.82 KB 34.82 KB UMD_PROFILING
react-dom.development.js +0.1% +0.1% 775.46 KB 776.37 KB 176.7 KB 176.91 KB NODE_DEV
react-dom.production.min.js 0.0% -0.0% 105.22 KB 105.22 KB 33.66 KB 33.66 KB NODE_PROD
react-dom.profiling.min.js 0.0% 0.0% 108.33 KB 108.33 KB 34.27 KB 34.27 KB NODE_PROFILING
ReactDOM-dev.js +0.1% +0.1% 798.3 KB 799.28 KB 177.83 KB 178.06 KB FB_WWW_DEV
ReactDOM-profiling.js 0.0% 0.0% 329.83 KB 329.83 KB 60.7 KB 60.7 KB FB_WWW_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.1% 781.42 KB 782.32 KB 178.37 KB 178.59 KB UMD_DEV
react-dom-unstable-fire.production.min.js 0.0% 0.0% 105.25 KB 105.25 KB 34.13 KB 34.13 KB UMD_PROD
react-dom-unstable-fire.profiling.min.js 0.0% -0.0% 108.26 KB 108.26 KB 34.83 KB 34.83 KB UMD_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.1% 775.8 KB 776.71 KB 176.84 KB 177.05 KB NODE_DEV
react-dom-unstable-fire.profiling.min.js 0.0% 0.0% 108.35 KB 108.35 KB 34.28 KB 34.28 KB NODE_PROFILING
ReactFire-dev.js +0.1% +0.1% 797.51 KB 798.49 KB 177.74 KB 177.97 KB FB_WWW_DEV
ReactFire-prod.js 0.0% -0.0% 312.44 KB 312.44 KB 56.99 KB 56.99 KB FB_WWW_PROD
react-dom-test-utils.production.min.js 0.0% -0.0% 10.27 KB 10.27 KB 3.8 KB 3.8 KB UMD_PROD
react-dom-test-utils.production.min.js 0.0% -0.0% 10.05 KB 10.05 KB 3.74 KB 3.73 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.28 KB 60.28 KB 15.79 KB 15.78 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.7 KB NODE_PROD
react-dom-server.browser.development.js +0.7% +0.7% 130.49 KB 131.4 KB 34.82 KB 35.06 KB UMD_DEV
react-dom-server.browser.development.js +0.7% +0.7% 126.62 KB 127.53 KB 33.9 KB 34.13 KB NODE_DEV
ReactDOMServer-dev.js +0.8% +0.7% 127.59 KB 128.58 KB 33.28 KB 33.52 KB FB_WWW_DEV
react-dom-server.node.development.js +0.7% +0.7% 128.68 KB 129.59 KB 34.45 KB 34.67 KB NODE_DEV
react-dom-server.node.production.min.js 0.0% -0.0% 19.99 KB 19.99 KB 7.59 KB 7.59 KB NODE_PROD
react-dom-unstable-fizz.node.development.js 0.0% +0.1% 3.7 KB 3.7 KB 1.42 KB 1.42 KB NODE_DEV

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +0.2% +0.2% 553.37 KB 554.28 KB 120.46 KB 120.69 KB UMD_DEV
react-art.production.min.js 0.0% -0.0% 97.06 KB 97.06 KB 29.9 KB 29.9 KB UMD_PROD
react-art.development.js +0.2% +0.2% 484.26 KB 485.17 KB 103.14 KB 103.36 KB NODE_DEV
react-art.production.min.js 0.0% -0.0% 61.98 KB 61.98 KB 19.02 KB 19.02 KB NODE_PROD
ReactART-dev.js +0.2% +0.2% 493.22 KB 494.19 KB 102.26 KB 102.48 KB FB_WWW_DEV

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactNativeRenderer-dev.js +0.2% +0.2% 618.99 KB 619.96 KB 132.53 KB 132.75 KB RN_FB_DEV
ReactNativeRenderer-dev.js +0.2% +0.2% 618.9 KB 619.87 KB 132.5 KB 132.72 KB RN_OSS_DEV
ReactNativeRenderer-prod.js 0.0% 0.0% 246.04 KB 246.04 KB 43.08 KB 43.08 KB RN_OSS_PROD
ReactFabric-dev.js +0.2% +0.2% 609.42 KB 610.39 KB 130.22 KB 130.44 KB RN_FB_DEV
ReactFabric-profiling.js 0.0% -0.0% 245.42 KB 245.42 KB 43.24 KB 43.24 KB RN_FB_PROFILING
ReactFabric-dev.js +0.2% +0.2% 609.32 KB 610.29 KB 130.17 KB 130.39 KB RN_OSS_DEV
ReactFabric-profiling.js 0.0% -0.0% 245.43 KB 245.43 KB 43.24 KB 43.23 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.2% 493.09 KB 494 KB 104.74 KB 104.98 KB UMD_DEV
react-test-renderer.production.min.js 0.0% 0.0% 62.99 KB 62.99 KB 19.32 KB 19.32 KB UMD_PROD
react-test-renderer.development.js +0.2% +0.2% 488.61 KB 489.52 KB 103.59 KB 103.84 KB NODE_DEV
react-test-renderer.production.min.js 0.0% 0.0% 62.64 KB 62.64 KB 19.09 KB 19.09 KB NODE_PROD
ReactTestRenderer-dev.js +0.2% +0.2% 498.52 KB 499.49 KB 103.18 KB 103.41 KB FB_WWW_DEV
react-test-renderer-shallow.production.min.js 0.0% 0.0% 11.73 KB 11.73 KB 3.65 KB 3.65 KB UMD_PROD
react-test-renderer-shallow.development.js 0.0% -0.0% 33.05 KB 33.05 KB 8.49 KB 8.49 KB NODE_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.0% 11.92 KB 11.92 KB 3.77 KB 3.76 KB NODE_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.development.js +0.2% +0.2% 482.69 KB 483.6 KB 101.73 KB 101.95 KB NODE_DEV
react-reconciler-persistent.development.js +0.2% +0.2% 480.68 KB 481.59 KB 100.93 KB 101.15 KB NODE_DEV
react-reconciler-reflection.production.min.js 0.0% -0.1% 2.7 KB 2.7 KB 1.23 KB 1.22 KB NODE_PROD

Generated by 🚫 dangerJS

'This can also happen due to a circular dependency, so ' +
'try moving the createContext() call to a separate file.';
} else if (typeof contextType !== 'object') {
addendum = ' However, it is set to ' + typeof contextType + '.';
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can add "a" here to make it a little clearer

addendum = ' However, it is set to a ' + typeof contextType + '.';

That way it reports "set to a string" instead of "set to string", which sounds better. Same goes for numbers, symbols, and functions.

} else if (contextType._context !== undefined) {
// <Context.Consumer>
addendum = ' Did you accidentally pass the Context.Consumer instead?';
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add another branch for arrays?

else if (Array.isArray(contextType)) {
  addendum = 'However, it is set to an array.'
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Seems very unlikely to happen

Copy link
Contributor

Choose a reason for hiding this comment

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

🤷‍♂️ seems like it could happen if you import the wrong value. I figured that if it happens it would be weird to see.

However, it is set to an object with keys {0, 1, 2, 3, 4, 5, 6...100000}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Arrays are pretty rare as export values so I don't think it's worth handling.

didWarnAboutInvalidateContextType.add(type);

let addendum = '';
if (contextType === null) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sometimes it is nice to be able to conditionally set it:

class Foo {
  static contextType = isServerEnvironment ? null : Context;
}

This provides no way to do that while also ensuring you stay declarative.

Is null really a common mistake? I'm guessing not since it's the only one you don't have a descriptive message for.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nope. Agree let’s omit it.

@gaearon gaearon merged commit 7ad7386 into facebook:master Mar 19, 2019
gaearon added a commit to gaearon/react that referenced this pull request Mar 27, 2019
* Improve warning for invalid class contextType

* Don't warn for null

* Grammar
@gaearon gaearon mentioned this pull request Mar 27, 2019
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

5 participants