Skip to content

Fix the misleading error message in production #11761

Closed
@gaearon

Description

@gaearon

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions