Skip to content

Commit

Permalink
Updated misleading error message in production environment when addin…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
skiritsis authored and ManasJayanth committed Jan 12, 2018
1 parent 623f10c commit 16a6050
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
12 changes: 8 additions & 4 deletions packages/react-dom/src/__tests__/ReactStatelessComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,14 @@ describe('ReactStatelessComponent', () => {
}).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.',
: // It happens because we don't save _owner in production for
// functional components.
'Element ref was specified as a string (me) but no owner was set. This could happen for one of' +
' the following reasons:\n' +
'1. You may be adding a ref to a functional component\n' +
"2. You may be adding a ref to a component that was not created inside a component's render method\n" +
'3. You have multiple copies of React loaded\n' +
'See https://fb.me/react-refs-must-have-owner for more information.',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ describe('when different React version is used with string ref', () => {
expect(() => {
ReactTestUtils.renderIntoDocument(<TextWithStringRef />);
}).toThrow(
'Element ref was specified as a string (foo) but no owner was set.' +
' You may have multiple copies of React loaded. (details: ' +
'https://fb.me/react-refs-must-have-owner).',
'Element ref was specified as a string (foo) but no owner was set. This could happen for one of' +
' the following reasons:\n' +
'1. You may be adding a ref to a functional component\n' +
"2. You may be adding a ref to a component that was not created inside a component's render method\n" +
'3. You have multiple copies of React loaded\n' +
'See https://fb.me/react-refs-must-have-owner for more information.',
);
});
});
9 changes: 6 additions & 3 deletions packages/react-dom/src/__tests__/refs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,12 @@ describe('creating element with ref in constructor', () => {
expect(function() {
ReactTestUtils.renderIntoDocument(<RefTest />);
}).toThrowError(
'Element ref was specified as a string (p) but no owner was ' +
'set. You may have multiple copies of React loaded. ' +
'(details: https://fb.me/react-refs-must-have-owner).',
'Element ref was specified as a string (p) but no owner was set. This could happen for one of' +
' the following reasons:\n' +
'1. You may be adding a ref to a functional component\n' +
"2. You may be adding a ref to a component that was not created inside a component's render method\n" +
'3. You have multiple copies of React loaded\n' +
'See https://fb.me/react-refs-must-have-owner for more information.',
);
});
});
9 changes: 6 additions & 3 deletions packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ function coerceRef(current: Fiber | null, element: ReactElement) {
);
invariant(
element._owner,
'Element ref was specified as a string (%s) but no owner was ' +
'set. You may have multiple copies of React loaded. ' +
'(details: https://fb.me/react-refs-must-have-owner).',
'Element ref was specified as a string (%s) but no owner was set. This could happen for one of' +
' the following reasons:\n' +
'1. You may be adding a ref to a functional component\n' +
"2. You may be adding a ref to a component that was not created inside a component's render method\n" +
'3. You have multiple copies of React loaded\n' +
'See https://fb.me/react-refs-must-have-owner for more information.',
mixedRef,
);
}
Expand Down

0 comments on commit 16a6050

Please sign in to comment.