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

add functional components warning about legacy context api #12892

Merged
merged 1 commit into from
May 23, 2018
Merged

add functional components warning about legacy context api #12892

merged 1 commit into from
May 23, 2018

Conversation

cyan33
Copy link
Contributor

@cyan33 cyan33 commented May 23, 2018

Looks like we're not catching functional components here. They can also have contextTypes and should also be warned about.

As is mentioned by @gaearon in the previous PR, functional components should also be warned if they have contextTypes. Previously it only warns in class components.

To reduce extraneous check, the logic is put into mountIndeterminateComponent.

@@ -600,6 +601,11 @@ function mountIndeterminateComponent(
didWarnAboutBadClass[componentName] = true;
}
}

if (workInProgress.mode & StrictMode) {
ReactStrictModeWarnings.recordLegacyContextWarning(workInProgress);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd expect Flow to error on this because we pass different number of arguments. Let's explicitly pass null when we don't have an instance?

Copy link
Contributor Author

@cyan33 cyan33 May 23, 2018

Choose a reason for hiding this comment

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

It didn't error on this, but I certainly agree on yours.

fiber.type.contextTypes != null ||
fiber.type.childContextTypes != null
fiber.type.childContextTypes != null ||
(instance && typeof instance.getChildContext === 'function')
Copy link
Collaborator

Choose a reason for hiding this comment

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

We try to avoid checks like something && in terms of explicit something !== null && when that argument is coming from our own code (and we know it's either null or an object).

@@ -864,7 +877,8 @@ describe('ReactStrictMode', () => {
'Warning: Legacy context API has been detected within a strict-mode tree: ' +
'\n in div (at **)' +
'\n in Root (at **)' +
'\n\nPlease update the following components: LegacyContextConsumer, LegacyContextProvider' +
'\n\nPlease update the following components: ' +
'FunctionalLegacyContextConsumer, LegacyContextConsumer, LegacyContextProvider' +
'\n\nLearn more about this warning here:' +
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you also add a test for factory component please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure will do

Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

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

LGTM

@cyan33 cyan33 merged commit 53852a8 into facebook:master May 23, 2018
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