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

Fix the misleading error message in production #11761

Closed
gaearon opened this issue Dec 4, 2017 · 5 comments
Closed

Fix the misleading error message in production #11761

gaearon opened this issue Dec 4, 2017 · 5 comments

Comments

@gaearon
Copy link
Collaborator

gaearon commented Dec 4, 2017

See this test case:

it('should throw on string refs in pure functions', () => {
function Child() {
return <div ref="me" />;
}
expect(function() {
ReactTestUtils.renderIntoDocument(<Child test="test" />);
}).toThrowError(
__DEV__
? 'Stateless function components cannot have refs.'
: // TODO: the different message in production seems like a bug.
// It happens because we don't save _owner in production for
// functional components. We should probably show a better message.
'Element ref was specified as a string (me) but no owner was set.',
);
});

Pretty sure the different message in production wasn't intentional, but is a consequence of ReactCurrentOwner.current not being set in production for functional components.

We need to see if it's possible to emit the same (useful) message both in development in production. If it's not possible, we need to change the production message to be more ambiguous instead of suggesting the wrong reason for the problem.

@extwiii
Copy link

extwiii commented Dec 4, 2017

What about this;
'Warning: Stateless function components cannot be given refs.Attempts to access this ref will fail.\n\nCheck the render method '.

Similar to it('should warn when given a string ref' test spec.

@gaearon
Copy link
Collaborator Author

gaearon commented Dec 4, 2017

I don't see a problem with that one. Do you?

@skiritsis
Copy link
Contributor

I wouldn't mind checking this if @extwiii does not want to.

@gaearon
Copy link
Collaborator Author

gaearon commented Dec 4, 2017

@skiritsis You got it!

gaearon pushed a commit that referenced this issue Jan 5, 2018
…g ref to a functional component (#11761) (#11782)

* Updated misleading error message in production environment when adding ref to a functional component

* Reverted changes to codes.json

* Updated error message
@gaearon
Copy link
Collaborator Author

gaearon commented Jan 5, 2018

#11782

@gaearon gaearon closed this as completed Jan 5, 2018
yenshih pushed a commit to yenshih/react that referenced this issue Jan 6, 2018
…g ref to a functional component (facebook#11761) (facebook#11782)

* Updated misleading error message in production environment when adding ref to a functional component

* Reverted changes to codes.json

* Updated error message
ManasJayanth pushed a commit to ManasJayanth/react that referenced this issue Jan 12, 2018
…g ref to a functional component (facebook#11761) (facebook#11782)

* Updated misleading error message in production environment when adding ref to a functional component

* Reverted changes to codes.json

* Updated error message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants