Skip to content

skip null ref warning for ReactTestRenderer - #7658

Merged
gaearon merged 1 commit into
react:masterfrom
aweary:fix-react-test-renderer-ref-warning
Sep 6, 2016
Merged

gaearon merged 1 commit into
react:masterfrom
aweary:fix-react-test-renderer-ref-warning

Conversation

@aweary

@aweary aweary commented Sep 5, 2016

Copy link
Copy Markdown
Contributor

Resolves #7645 as far as the warning goes--assuming that toJSON isn't going to be implemented in other renderers.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 87.192% when pulling b89824f on Aweary:fix-react-test-renderer-ref-warning into 6a525fd on facebook:master.

if (__DEV__) {
var componentName = component && component.getName ?
component.getName() : 'a component';
warning(publicComponentInstance != null,

@gaearon gaearon Sep 5, 2016

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we check this._compositeType instead, and only warn for stateless functional components? It seems like this would solve the issue by making the warning more specific without hardcoding information about test renderer. Another upside is that the warning would still fire if you try to use refs with stateless components incorrectly while using test renderer for example.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oooo, I didn't know about _compositeType, thanks! 🎉

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 87.192% when pulling 11315dc on Aweary:fix-react-test-renderer-ref-warning into 6a525fd on facebook:master.

@gaearon gaearon added this to the 15-next milestone Sep 6, 2016
@gaearon gaearon assigned gaearon and unassigned sophiebits Sep 6, 2016
@gaearon
gaearon merged commit 06ea71d into react:master Sep 6, 2016
@gaearon

gaearon commented Sep 6, 2016

Copy link
Copy Markdown
Contributor

Looks great. Thanks.

warning(publicComponentInstance != null,
warning(
publicComponentInstance != null ||
component._compositeType !== CompositeTypes.StatelessFunctional,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note: this is a little sketchy because component might not be a ReactCompositeComponent (and thus wouldn't have _compositeType), but I suppose it is fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It should still suppress the warning correctly though, right? Since it wouldn't be a SFC if there wasn't a _compositeType anyways. I can add an additional check to make it safer if you'd like

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right, it would. I avoid polymorphic accesses like this whenever possible because they're slower but I'm not sure there's a better option here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree, sorry I missed this. Should we add a check for typeof component._currentElement.type === 'function'?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

component._currentElement is still polymorphic. We'd probably need to pass the component type along with the component instance if we wanted to avoid this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is fine as-is, just wanted to note it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, I understand this now. Different hidden class? Seems like we do this in many places anyway but thanks for explaining.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah.

@zpao zpao modified the milestones: 15.3.2, 15-next Sep 8, 2016
zpao pushed a commit that referenced this pull request Sep 15, 2016
mrizwanashiq pushed a commit to mrizwanashiq/react that referenced this pull request Jun 25, 2026
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.

6 participants