Skip to content

Commit

Permalink
Change string refs in function component message (#18031)
Browse files Browse the repository at this point in the history
This should refer to string refs specifically. The forwardRef part doesn't
make any sense in this case. I think this was just an oversight.
  • Loading branch information
sebmarkbage committed Feb 21, 2020
1 parent 65bbda7 commit 2c4221c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('ReactFunctionComponent', () => {
ReactTestUtils.renderIntoDocument(<Child test="test" />);
}).toThrowError(
__DEV__
? 'Function components cannot have refs.'
? 'Function components cannot have string refs. We recommend using useRef() instead.'
: // It happens because we don't save _owner in production for
// function components.
'Element ref was specified as a string (me) but no owner was set. This could happen for one of' +
Expand Down
6 changes: 4 additions & 2 deletions packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@ function coerceRef(
const ownerFiber = ((owner: any): Fiber);
invariant(
ownerFiber.tag === ClassComponent,
'Function components cannot have refs. ' +
'Did you mean to use React.forwardRef()?',
'Function components cannot have string refs. ' +
'We recommend using useRef() instead. ' +
'Learn more about using refs safely here: ' +
'https://fb.me/react-strict-mode-string-ref',
);
inst = ownerFiber.stateNode;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/error-codes/codes.json
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@
"306": "Element type is invalid. Received a promise that resolves to: %s. Lazy element type must resolve to a class or function.%s",
"307": "Hooks can only be called inside the body of a function component. (https://fb.me/react-invalid-hook-call)",
"308": "Context can only be read while React is rendering. In classes, you can read it in the render method or getDerivedStateFromProps. In function components, you can read it directly in the function body, but not inside Hooks like useReducer() or useMemo().",
"309": "Function components cannot have refs. Did you mean to use React.forwardRef()?",
"309": "Function components cannot have string refs. We recommend using useRef() instead. Learn more about using refs safely here: https://fb.me/react-strict-mode-string-ref",
"310": "Rendered more hooks than during the previous render.",
"311": "Should have a queue. This is likely a bug in React. Please file an issue.",
"312": "Rendered more hooks than during the previous render",
Expand Down

0 comments on commit 2c4221c

Please sign in to comment.