Skip to content

Commit

Permalink
Mention forwardRef() in <Fn ref={...} /> errors and warnings (#14644)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Jan 21, 2019
1 parent a1414e8 commit baa6d40
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
12 changes: 9 additions & 3 deletions packages/react-dom/src/__tests__/ReactFunctionComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ describe('ReactFunctionComponent', () => {
ReactTestUtils.renderIntoDocument(<ParentUsingStringRef />),
).toWarnDev(
'Warning: Function components cannot be given refs. ' +
'Attempts to access this ref will fail.\n\nCheck the render method ' +
'Attempts to access this ref will fail. ' +
'Did you mean to use React.forwardRef()?\n\n' +
'Check the render method ' +
'of `ParentUsingStringRef`.\n' +
' in FunctionComponent (at **)\n' +
' in div (at **)\n' +
Expand Down Expand Up @@ -228,7 +230,9 @@ describe('ReactFunctionComponent', () => {
ReactTestUtils.renderIntoDocument(<ParentUsingFunctionRef />),
).toWarnDev(
'Warning: Function components cannot be given refs. ' +
'Attempts to access this ref will fail.\n\nCheck the render method ' +
'Attempts to access this ref will fail. ' +
'Did you mean to use React.forwardRef()?\n\n' +
'Check the render method ' +
'of `ParentUsingFunctionRef`.\n' +
' in FunctionComponent (at **)\n' +
' in div (at **)\n' +
Expand Down Expand Up @@ -332,7 +336,9 @@ describe('ReactFunctionComponent', () => {

expect(() => ReactTestUtils.renderIntoDocument(<Parent />)).toWarnDev(
'Warning: Function components cannot be given refs. ' +
'Attempts to access this ref will fail.\n\nCheck the render method ' +
'Attempts to access this ref will fail. ' +
'Did you mean to use React.forwardRef()?\n\n' +
'Check the render method ' +
'of `Parent`.\n' +
' in Child (at **)\n' +
' in Parent (at **)',
Expand Down
3 changes: 2 additions & 1 deletion packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ function coerceRef(
const ownerFiber = ((owner: any): Fiber);
invariant(
ownerFiber.tag === ClassComponent,
'Function components cannot have refs.',
'Function components cannot have refs. ' +
'Did you mean to use React.forwardRef()?',
);
inst = ownerFiber.stateNode;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,8 @@ function validateFunctionComponentInDev(workInProgress: Fiber, Component: any) {
warning(
false,
'Function components cannot be given refs. ' +
'Attempts to access this ref will fail.%s',
'Attempts to access this ref will fail. ' +
'Did you mean to use React.forwardRef()?%s',
info,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,9 @@ describe('ReactIncrementalSideEffects', () => {
ReactNoop.render(<Foo show={true} />);
expect(ReactNoop.flush).toWarnDev(
'Warning: Function components cannot be given refs. ' +
'Attempts to access this ref will fail.\n\nCheck the render method ' +
'Attempts to access this ref will fail. ' +
'Did you mean to use React.forwardRef()?\n\n' +
'Check the render method ' +
'of `Foo`.\n' +
' in FunctionComponent (at **)\n' +
' in div (at **)\n' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ describe('ReactTestRenderer', () => {
ReactTestRenderer.create(<Baz />);
expect(() => ReactTestRenderer.create(<Foo />)).toWarnDev(
'Warning: Function components cannot be given refs. Attempts ' +
'to access this ref will fail.\n\nCheck the render method of `Foo`.\n' +
'to access this ref will fail. ' +
'Did you mean to use React.forwardRef()?\n\n' +
'Check the render method of `Foo`.\n' +
' in Bar (at **)\n' +
' in Foo (at **)',
);
Expand Down

0 comments on commit baa6d40

Please sign in to comment.