Skip to content

Commit

Permalink
Convert string ref props to callback props (#28398)
Browse files Browse the repository at this point in the history
When enableRefAsProp is on, we should always use the props as the source
of truth for refs. Not a field on the fiber.

In the case of string refs, this presents a problem, because string refs
are not passed around internally as strings; they are converted to
callback refs. The ref used by the reconciler is not the same as the one
the user provided.

But since this is a deprecated feature anyway, what we can do is clone
the props object and replace it with the internal callback ref. Then we
can continue to use the props object as the source of truth.

This means the internal callback ref will leak into userspace. The
receiving component will receive a callback ref even though the parent
passed a string. Which is weird, but again, this is a deprecated
feature, and we're only leaving it around behind a flag so that Meta can
keep using string refs temporarily while they finish migrating their
codebase.

DiffTrain build for commit dc30644.
  • Loading branch information
acdlite committed Feb 21, 2024
1 parent bb2b69a commit 3513824
Show file tree
Hide file tree
Showing 13 changed files with 706 additions and 703 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<1d3d484449a45d9d48c574d191f78a9b>>
* @generated SignedSource<<c84520e2b9d33ee51d806026be234a8a>>
*/

"use strict";
Expand Down Expand Up @@ -5039,127 +5039,138 @@ if (__DEV__) {
return trackUsedThenable(thenableState$1, thenable, index);
}

function coerceRef(returnFiber, current, element) {
var mixedRef;

{
// Old behavior.
mixedRef = element.ref;
}

if (
mixedRef !== null &&
typeof mixedRef !== "function" &&
typeof mixedRef !== "object"
) {
{
if (
// Will already throw with "Function components cannot have string refs"
!(element._owner && element._owner.tag !== ClassComponent) && // Will already warn with "Function components cannot be given refs"
!(
typeof element.type === "function" && !isReactClass(element.type)
) && // Will already throw with "Element ref was specified as a string (someStringRef) but no owner was set"
element._owner
) {
var componentName =
getComponentNameFromFiber(returnFiber) || "Component";

if (!didWarnAboutStringRefs[componentName]) {
error(
'Component "%s" contains the string ref "%s". Support for string refs ' +
"will be removed in a future major release. We recommend using " +
"useRef() or createRef() instead. " +
"Learn more about using refs safely here: " +
"https://reactjs.org/link/strict-mode-string-ref",
componentName,
mixedRef
);
function convertStringRefToCallbackRef(
returnFiber,
current,
element,
mixedRef
) {
var owner = element._owner;

didWarnAboutStringRefs[componentName] = true;
}
}
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."
);
}

if (element._owner) {
var owner = element._owner;
var inst;
throw new Error(
"Element ref was specified as a string (" +
mixedRef +
") 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" +
"3. You have multiple copies of React loaded\n" +
"See https://reactjs.org/link/refs-must-have-owner for more information."
);
}

if (owner) {
var ownerFiber = owner;
if (owner.tag !== ClassComponent) {
throw new Error(
"Function components cannot have string refs. " +
"We recommend using useRef() instead. " +
"Learn more about using refs safely here: " +
"https://reactjs.org/link/strict-mode-string-ref"
);
} // 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 (ownerFiber.tag !== ClassComponent) {
throw new Error(
"Function components cannot have string refs. " +
"We recommend using useRef() instead. " +
"Learn more about using refs safely here: " +
"https://reactjs.org/link/strict-mode-string-ref"
);
}
{
checkPropStringCoercion(mixedRef, "ref");
}

inst = ownerFiber.stateNode;
}
var stringRef = "" + mixedRef;

if (!inst) {
throw new Error(
"Missing owner for string ref " +
mixedRef +
". This error is likely caused by a " +
"bug in React. Please file an issue."
);
} // Assigning this to a const so Flow knows it won't change in the closure
{
if (
// Will already warn with "Function components cannot be given refs"
!(typeof element.type === "function" && !isReactClass(element.type))
) {
var componentName =
getComponentNameFromFiber(returnFiber) || "Component";

var resolvedInst = inst;
if (!didWarnAboutStringRefs[componentName]) {
error(
'Component "%s" contains the string ref "%s". Support for string refs ' +
"will be removed in a future major release. We recommend using " +
"useRef() or createRef() instead. " +
"Learn more about using refs safely here: " +
"https://reactjs.org/link/strict-mode-string-ref",
componentName,
stringRef
);

{
checkPropStringCoercion(mixedRef, "ref");
didWarnAboutStringRefs[componentName] = true;
}
}
}

var stringRef = "" + mixedRef; // Check if previous string ref matches new string ref
var inst = owner.stateNode;

if (
current !== null &&
current.ref !== null &&
typeof current.ref === "function" &&
current.ref._stringRef === stringRef
) {
return current.ref;
}
if (!inst) {
throw new Error(
"Missing owner for string ref " +
stringRef +
". This error is likely caused by a " +
"bug in React. Please file an issue."
);
} // Check if previous string ref matches new string ref

var ref = function (value) {
var refs = resolvedInst.refs;
if (
current !== null &&
current.ref !== null &&
typeof current.ref === "function" &&
current.ref._stringRef === stringRef
) {
// Reuse the existing string ref
var currentRef = current.ref;
return currentRef;
} // Create a new string ref

if (value === null) {
delete refs[stringRef];
} else {
refs[stringRef] = value;
}
};
var ref = function (value) {
var refs = inst.refs;

ref._stringRef = stringRef;
return ref;
if (value === null) {
delete refs[stringRef];
} else {
if (typeof mixedRef !== "string") {
throw new Error(
"Expected ref to be a function, a string, an object returned by React.createRef(), or null."
);
}

if (!element._owner) {
throw new Error(
"Element ref was specified as a string (" +
mixedRef +
") 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" +
"3. You have multiple copies of React loaded\n" +
"See https://reactjs.org/link/refs-must-have-owner for more information."
);
}
refs[stringRef] = value;
}
};

ref._stringRef = stringRef;
return ref;
}

function coerceRef(returnFiber, current, workInProgress, element) {
var mixedRef;

{
// Old behavior.
mixedRef = element.ref;
}

return mixedRef;
var coercedRef;

if (
mixedRef !== null &&
typeof mixedRef !== "function" &&
typeof mixedRef !== "object"
) {
// Assume this is a string ref. If it's not, then this will throw an error
// to the user.
coercedRef = convertStringRefToCallbackRef(
returnFiber,
current,
element,
mixedRef
);
} else {
coercedRef = mixedRef;
} // TODO: If enableRefAsProp is on, we shouldn't use the `ref` field. We
// should always read the ref from the prop.

workInProgress.ref = coercedRef;
}

function throwOnInvalidObjectType(returnFiber, newChild) {
Expand Down Expand Up @@ -5415,7 +5426,7 @@ if (__DEV__) {
) {
// Move based on index
var existing = useFiber(current, element.props);
existing.ref = coerceRef(returnFiber, current, element);
coerceRef(returnFiber, current, existing, element);
existing.return = returnFiber;

{
Expand All @@ -5428,7 +5439,7 @@ if (__DEV__) {
} // Insert

var created = createFiberFromElement(element, returnFiber.mode, lanes);
created.ref = coerceRef(returnFiber, current, element);
coerceRef(returnFiber, current, created, element);
created.return = returnFiber;

{
Expand Down Expand Up @@ -5534,7 +5545,7 @@ if (__DEV__) {
lanes
);

_created.ref = coerceRef(returnFiber, null, newChild);
coerceRef(returnFiber, null, _created, newChild);
_created.return = returnFiber;

{
Expand Down Expand Up @@ -6397,7 +6408,7 @@ if (__DEV__) {

var _existing = useFiber(child, element.props);

_existing.ref = coerceRef(returnFiber, child, element);
coerceRef(returnFiber, child, _existing, element);
_existing.return = returnFiber;

{
Expand Down Expand Up @@ -6439,7 +6450,7 @@ if (__DEV__) {
lanes
);

_created4.ref = coerceRef(returnFiber, currentFirstChild, element);
coerceRef(returnFiber, currentFirstChild, _created4, element);
_created4.return = returnFiber;

{
Expand Down Expand Up @@ -25696,7 +25707,7 @@ if (__DEV__) {
return root;
}

var ReactVersion = "18.3.0-canary-ddd736d25-20240221";
var ReactVersion = "18.3.0-canary-dc30644ca-20240221";

// Might add PROFILE later.

Expand Down
Loading

0 comments on commit 3513824

Please sign in to comment.