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

skip null ref warning for ReactTestRenderer #7658

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,9 @@ var ReactCompositeComponent = {
if (__DEV__) {
var componentName = component && component.getName ?
component.getName() : 'a component';
warning(publicComponentInstance != null,
warning(
Copy link
Collaborator

@gaearon gaearon Sep 5, 2016

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
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! 🎉

publicComponentInstance != null ||
component._compositeType !== CompositeTypes.StatelessFunctional,
Copy link
Collaborator

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
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
Collaborator

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
Collaborator

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
Collaborator

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
Collaborator

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
Collaborator

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
Collaborator

Choose a reason for hiding this comment

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

Yeah.

'Stateless function components cannot be given refs ' +
'(See ref "%s" in %s created by %s). ' +
'Attempts to access this ref will fail.',
Expand Down
25 changes: 25 additions & 0 deletions src/renderers/testing/__tests__/ReactTestRenderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ describe('ReactTestRenderer', function() {
expect(log).toEqual([null]);
});

it('warns correctly for refs on SFCs', function() {
spyOn(console, 'error');
function Bar() {
return <div>Hello, world</div>
}
class Foo extends React.Component {
render() {
return <Bar ref="foo" />
}
}
class Baz extends React.Component {
render() {
return <div ref="baz" />
}
}
ReactTestRenderer.create(<Baz />);
ReactTestRenderer.create(<Foo />);
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'Stateless function components cannot be given refs ' +
'(See ref "foo" in Bar created by Foo). ' +
'Attempts to access this ref will fail.'
);
});

it('supports error boundaries', function() {
var log = [];
class Angry extends React.Component {
Expand Down