-
Notifications
You must be signed in to change notification settings - Fork 47.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move ref type check to receiver #28464
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,17 +157,17 @@ function convertStringRefToCallbackRef( | |
returnFiber: Fiber, | ||
current: Fiber | null, | ||
element: ReactElement, | ||
mixedRef: any, | ||
mixedRef: string | number | boolean, | ||
): CoercedStringRef { | ||
if (__DEV__) { | ||
checkPropStringCoercion(mixedRef, 'ref'); | ||
} | ||
const stringRef = '' + (mixedRef: any); | ||
|
||
const owner: ?Fiber = (element._owner: any); | ||
if (!owner) { | ||
if (typeof mixedRef !== 'string') { | ||
throw new Error( | ||
'Expected ref to be a function, a string, an object returned by React.createRef(), or null.', | ||
); | ||
} | ||
throw new Error( | ||
`Element ref was specified as a string (${mixedRef}) but no owner was set. This could happen for one of` + | ||
`Element ref was specified as a string (${stringRef}) but no owner was set. This could happen for one of` + | ||
' the following reasons:\n' + | ||
'1. You may be adding a ref to a function component\n' + | ||
"2. You may be adding a ref to a component that was not created inside a component's render method\n" + | ||
|
@@ -184,13 +184,6 @@ function convertStringRefToCallbackRef( | |
); | ||
} | ||
|
||
// At this point, we know the ref isn't an object or function but it could | ||
// be a number. Coerce it to a string. | ||
if (__DEV__) { | ||
checkPropStringCoercion(mixedRef, 'ref'); | ||
} | ||
const stringRef = '' + mixedRef; | ||
|
||
if (__DEV__) { | ||
if ( | ||
// Will already warn with "Function components cannot be given refs" | ||
|
@@ -267,12 +260,10 @@ function coerceRef( | |
let coercedRef; | ||
if ( | ||
!disableStringRefs && | ||
mixedRef !== null && | ||
typeof mixedRef !== 'function' && | ||
typeof mixedRef !== 'object' | ||
(typeof mixedRef === 'string' || | ||
typeof mixedRef === 'number' || | ||
typeof mixedRef === 'boolean') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a subtle change because we used to coerce any non-function, non-object type to a string, but now we explicitly check for only these types. It's only when string refs are not disabled, though. |
||
) { | ||
// Assume this is a string ref. If it's not, then this will throw an error | ||
// to the user. | ||
coercedRef = convertStringRefToCallbackRef( | ||
returnFiber, | ||
current, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -280,7 +280,7 @@ | |
"281": "Finished root should have a work-in-progress. This error is likely caused by a bug in React. Please file an issue.", | ||
"282": "If the root does not have an updateQueue, we should have already bailed out. This error is likely caused by a bug in React. Please file an issue.", | ||
"283": "Element type is invalid. Received a promise that resolves to: %s. Promise elements must resolve to a class or function.", | ||
"284": "Expected ref to be a function, a string, an object returned by React.createRef(), or null.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we give this a new error code somehow so that older versions of React decode an error message with their respective ref type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did it this way intentionally for log continuity. The new message isn't incorrect for older versions, per se. It doesn't list "string" as a valid type but that's fine because we'd rather then use one of the other ones anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That said I also think it's fine if we make a new error code. Generally I try to avoid creating new ones if the change isn't significant, though. |
||
"284": "Expected ref to be a function, an object returned by React.createRef(), or undefined/null.", | ||
"285": "The root failed to unmount after an error. This is likely a bug in React. Please file an issue.", | ||
"286": "%s(...): the first argument must be a React class instance. Instead received: %s.", | ||
"287": "It is not supported to run the profiling version of a renderer (for example, `react-dom/profiling`) without also replacing the `schedule/tracking` module with `schedule/tracking-profiling`. Your bundler might have a setting for aliasing both modules. Learn more at https://reactjs.org/link/profiling", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hhg.