Skip to content

Commit baa6d40

Browse files
authored
Mention forwardRef() in <Fn ref={...} /> errors and warnings (#14644)
1 parent a1414e8 commit baa6d40

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

packages/react-dom/src/__tests__/ReactFunctionComponent-test.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ describe('ReactFunctionComponent', () => {
192192
ReactTestUtils.renderIntoDocument(<ParentUsingStringRef />),
193193
).toWarnDev(
194194
'Warning: Function components cannot be given refs. ' +
195-
'Attempts to access this ref will fail.\n\nCheck the render method ' +
195+
'Attempts to access this ref will fail. ' +
196+
'Did you mean to use React.forwardRef()?\n\n' +
197+
'Check the render method ' +
196198
'of `ParentUsingStringRef`.\n' +
197199
' in FunctionComponent (at **)\n' +
198200
' in div (at **)\n' +
@@ -228,7 +230,9 @@ describe('ReactFunctionComponent', () => {
228230
ReactTestUtils.renderIntoDocument(<ParentUsingFunctionRef />),
229231
).toWarnDev(
230232
'Warning: Function components cannot be given refs. ' +
231-
'Attempts to access this ref will fail.\n\nCheck the render method ' +
233+
'Attempts to access this ref will fail. ' +
234+
'Did you mean to use React.forwardRef()?\n\n' +
235+
'Check the render method ' +
232236
'of `ParentUsingFunctionRef`.\n' +
233237
' in FunctionComponent (at **)\n' +
234238
' in div (at **)\n' +
@@ -332,7 +336,9 @@ describe('ReactFunctionComponent', () => {
332336

333337
expect(() => ReactTestUtils.renderIntoDocument(<Parent />)).toWarnDev(
334338
'Warning: Function components cannot be given refs. ' +
335-
'Attempts to access this ref will fail.\n\nCheck the render method ' +
339+
'Attempts to access this ref will fail. ' +
340+
'Did you mean to use React.forwardRef()?\n\n' +
341+
'Check the render method ' +
336342
'of `Parent`.\n' +
337343
' in Child (at **)\n' +
338344
' in Parent (at **)',

packages/react-reconciler/src/ReactChildFiber.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ function coerceRef(
138138
const ownerFiber = ((owner: any): Fiber);
139139
invariant(
140140
ownerFiber.tag === ClassComponent,
141-
'Function components cannot have refs.',
141+
'Function components cannot have refs. ' +
142+
'Did you mean to use React.forwardRef()?',
142143
);
143144
inst = ownerFiber.stateNode;
144145
}

packages/react-reconciler/src/ReactFiberBeginWork.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,8 @@ function validateFunctionComponentInDev(workInProgress: Fiber, Component: any) {
12931293
warning(
12941294
false,
12951295
'Function components cannot be given refs. ' +
1296-
'Attempts to access this ref will fail.%s',
1296+
'Attempts to access this ref will fail. ' +
1297+
'Did you mean to use React.forwardRef()?%s',
12971298
info,
12981299
);
12991300
}

packages/react-reconciler/src/__tests__/ReactIncrementalSideEffects-test.internal.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,9 @@ describe('ReactIncrementalSideEffects', () => {
12021202
ReactNoop.render(<Foo show={true} />);
12031203
expect(ReactNoop.flush).toWarnDev(
12041204
'Warning: Function components cannot be given refs. ' +
1205-
'Attempts to access this ref will fail.\n\nCheck the render method ' +
1205+
'Attempts to access this ref will fail. ' +
1206+
'Did you mean to use React.forwardRef()?\n\n' +
1207+
'Check the render method ' +
12061208
'of `Foo`.\n' +
12071209
' in FunctionComponent (at **)\n' +
12081210
' in div (at **)\n' +

packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ describe('ReactTestRenderer', () => {
282282
ReactTestRenderer.create(<Baz />);
283283
expect(() => ReactTestRenderer.create(<Foo />)).toWarnDev(
284284
'Warning: Function components cannot be given refs. Attempts ' +
285-
'to access this ref will fail.\n\nCheck the render method of `Foo`.\n' +
285+
'to access this ref will fail. ' +
286+
'Did you mean to use React.forwardRef()?\n\n' +
287+
'Check the render method of `Foo`.\n' +
286288
' in Bar (at **)\n' +
287289
' in Foo (at **)',
288290
);

0 commit comments

Comments
 (0)