Skip to content

Commit c1210a1

Browse files
committed
[string-refs] make disableStringRefs a dynamic www flag (#31175)
DiffTrain build for [75dd053](75dd053)
1 parent 0ccec15 commit c1210a1

37 files changed

+905
-714
lines changed

compiled/facebook-www/JSXDEVRuntime-dev.classic.js

+52-41
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ __DEV__ &&
466466
}
467467
function warnIfStringRefCannotBeAutoConverted(config, self) {
468468
var owner;
469-
"string" === typeof config.ref &&
469+
!disableStringRefs &&
470+
"string" === typeof config.ref &&
470471
(owner = getOwner()) &&
471472
self &&
472473
owner.stateNode !== self &&
@@ -639,18 +640,23 @@ __DEV__ &&
639640
(checkKeyStringCoercion(maybeKey), (children = "" + maybeKey));
640641
hasValidKey(config) &&
641642
(checkKeyStringCoercion(config.key), (children = "" + config.key));
642-
hasValidRef(config) && warnIfStringRefCannotBeAutoConverted(config, self);
643-
if ("ref" in config || "key" in config) {
643+
hasValidRef(config) &&
644+
(disableStringRefs ||
645+
warnIfStringRefCannotBeAutoConverted(config, self));
646+
if (
647+
(!enableFastJSXWithoutStringRefs && "ref" in config) ||
648+
"key" in config
649+
) {
644650
maybeKey = {};
645651
for (var propName in config)
646652
"key" !== propName &&
647-
("ref" === propName
648-
? (maybeKey.ref = coerceStringRef(
653+
(disableStringRefs || "ref" !== propName
654+
? (maybeKey[propName] = config[propName])
655+
: (maybeKey.ref = coerceStringRef(
649656
config[propName],
650657
getOwner(),
651658
type
652-
))
653-
: (maybeKey[propName] = config[propName]));
659+
)));
654660
} else maybeKey = config;
655661
if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) {
656662
config = type.defaultProps;
@@ -756,6 +762,7 @@ __DEV__ &&
756762
return info;
757763
}
758764
function coerceStringRef(mixedRef, owner, type) {
765+
if (disableStringRefs) return mixedRef;
759766
if ("string" !== typeof mixedRef)
760767
if ("number" === typeof mixedRef || "boolean" === typeof mixedRef)
761768
willCoercionThrow(mixedRef) &&
@@ -774,44 +781,47 @@ __DEV__ &&
774781
return callback;
775782
}
776783
function stringRefAsCallbackRef(stringRef, type, owner, value) {
777-
if (!owner)
778-
throw Error(
779-
"Element ref was specified as a string (" +
780-
stringRef +
781-
") but no owner was set. This could happen for one of the following reasons:\n1. You may be adding a ref to a function component\n2. You may be adding a ref to a component that was not created inside a component's render method\n3. You have multiple copies of React loaded\nSee https://react.dev/link/refs-must-have-owner for more information."
782-
);
783-
if (1 !== owner.tag)
784-
throw Error(
785-
"Function components cannot have string refs. We recommend using useRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref"
786-
);
787-
if (
788-
"function" !== typeof type ||
789-
(type.prototype && type.prototype.isReactComponent)
790-
)
791-
(type = getComponentNameFromFiber(owner) || "Component"),
792-
didWarnAboutStringRefs[type] ||
793-
(enableLogStringRefsProd &&
794-
enableLogStringRefsProd(type, stringRef),
795-
error(
796-
'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://react.dev/link/strict-mode-string-ref',
797-
type,
798-
stringRef
799-
),
800-
(didWarnAboutStringRefs[type] = !0));
801-
owner = owner.stateNode;
802-
if (!owner)
803-
throw Error(
804-
"Missing owner for string ref " +
805-
stringRef +
806-
". This error is likely caused by a bug in React. Please file an issue."
807-
);
808-
owner = owner.refs;
809-
null === value ? delete owner[stringRef] : (owner[stringRef] = value);
784+
if (!disableStringRefs) {
785+
if (!owner)
786+
throw Error(
787+
"Element ref was specified as a string (" +
788+
stringRef +
789+
") but no owner was set. This could happen for one of the following reasons:\n1. You may be adding a ref to a function component\n2. You may be adding a ref to a component that was not created inside a component's render method\n3. You have multiple copies of React loaded\nSee https://react.dev/link/refs-must-have-owner for more information."
790+
);
791+
if (1 !== owner.tag)
792+
throw Error(
793+
"Function components cannot have string refs. We recommend using useRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref"
794+
);
795+
if (
796+
"function" !== typeof type ||
797+
(type.prototype && type.prototype.isReactComponent)
798+
)
799+
(type = getComponentNameFromFiber(owner) || "Component"),
800+
didWarnAboutStringRefs[type] ||
801+
(enableLogStringRefsProd &&
802+
enableLogStringRefsProd(type, stringRef),
803+
error(
804+
'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://react.dev/link/strict-mode-string-ref',
805+
type,
806+
stringRef
807+
),
808+
(didWarnAboutStringRefs[type] = !0));
809+
owner = owner.stateNode;
810+
if (!owner)
811+
throw Error(
812+
"Missing owner for string ref " +
813+
stringRef +
814+
". This error is likely caused by a bug in React. Please file an issue."
815+
);
816+
owner = owner.refs;
817+
null === value ? delete owner[stringRef] : (owner[stringRef] = value);
818+
}
810819
}
811820
var React = require("react"),
812821
dynamicFeatureFlags = require("ReactFeatureFlags"),
813822
disableDefaultPropsExceptForClasses =
814823
dynamicFeatureFlags.disableDefaultPropsExceptForClasses,
824+
disableStringRefs = dynamicFeatureFlags.disableStringRefs,
815825
enableDebugTracing = dynamicFeatureFlags.enableDebugTracing,
816826
enableLogStringRefsProd = dynamicFeatureFlags.enableLogStringRefsProd,
817827
enableRenderableContext = dynamicFeatureFlags.enableRenderableContext,
@@ -867,7 +877,8 @@ __DEV__ &&
867877
specialPropKeyWarningShown;
868878
var didWarnAboutStringRefs = {};
869879
var didWarnAboutElementRef = {};
870-
var didWarnAboutKeySpread = {},
880+
var enableFastJSXWithoutStringRefs = disableStringRefs,
881+
didWarnAboutKeySpread = {},
871882
ownerHasKeyUseWarning = {};
872883
exports.Fragment = REACT_FRAGMENT_TYPE;
873884
exports.jsxDEV = function (

compiled/facebook-www/JSXDEVRuntime-dev.modern.js

+52-41
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ __DEV__ &&
463463
}
464464
function warnIfStringRefCannotBeAutoConverted(config, self) {
465465
var owner;
466-
"string" === typeof config.ref &&
466+
!disableStringRefs &&
467+
"string" === typeof config.ref &&
467468
(owner = getOwner()) &&
468469
self &&
469470
owner.stateNode !== self &&
@@ -636,18 +637,23 @@ __DEV__ &&
636637
(checkKeyStringCoercion(maybeKey), (children = "" + maybeKey));
637638
hasValidKey(config) &&
638639
(checkKeyStringCoercion(config.key), (children = "" + config.key));
639-
hasValidRef(config) && warnIfStringRefCannotBeAutoConverted(config, self);
640-
if ("ref" in config || "key" in config) {
640+
hasValidRef(config) &&
641+
(disableStringRefs ||
642+
warnIfStringRefCannotBeAutoConverted(config, self));
643+
if (
644+
(!enableFastJSXWithoutStringRefs && "ref" in config) ||
645+
"key" in config
646+
) {
641647
maybeKey = {};
642648
for (var propName in config)
643649
"key" !== propName &&
644-
("ref" === propName
645-
? (maybeKey.ref = coerceStringRef(
650+
(disableStringRefs || "ref" !== propName
651+
? (maybeKey[propName] = config[propName])
652+
: (maybeKey.ref = coerceStringRef(
646653
config[propName],
647654
getOwner(),
648655
type
649-
))
650-
: (maybeKey[propName] = config[propName]));
656+
)));
651657
} else maybeKey = config;
652658
if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) {
653659
config = type.defaultProps;
@@ -753,6 +759,7 @@ __DEV__ &&
753759
return info;
754760
}
755761
function coerceStringRef(mixedRef, owner, type) {
762+
if (disableStringRefs) return mixedRef;
756763
if ("string" !== typeof mixedRef)
757764
if ("number" === typeof mixedRef || "boolean" === typeof mixedRef)
758765
willCoercionThrow(mixedRef) &&
@@ -771,44 +778,47 @@ __DEV__ &&
771778
return callback;
772779
}
773780
function stringRefAsCallbackRef(stringRef, type, owner, value) {
774-
if (!owner)
775-
throw Error(
776-
"Element ref was specified as a string (" +
777-
stringRef +
778-
") but no owner was set. This could happen for one of the following reasons:\n1. You may be adding a ref to a function component\n2. You may be adding a ref to a component that was not created inside a component's render method\n3. You have multiple copies of React loaded\nSee https://react.dev/link/refs-must-have-owner for more information."
779-
);
780-
if (1 !== owner.tag)
781-
throw Error(
782-
"Function components cannot have string refs. We recommend using useRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref"
783-
);
784-
if (
785-
"function" !== typeof type ||
786-
(type.prototype && type.prototype.isReactComponent)
787-
)
788-
(type = getComponentNameFromFiber(owner) || "Component"),
789-
didWarnAboutStringRefs[type] ||
790-
(enableLogStringRefsProd &&
791-
enableLogStringRefsProd(type, stringRef),
792-
error(
793-
'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://react.dev/link/strict-mode-string-ref',
794-
type,
795-
stringRef
796-
),
797-
(didWarnAboutStringRefs[type] = !0));
798-
owner = owner.stateNode;
799-
if (!owner)
800-
throw Error(
801-
"Missing owner for string ref " +
802-
stringRef +
803-
". This error is likely caused by a bug in React. Please file an issue."
804-
);
805-
owner = owner.refs;
806-
null === value ? delete owner[stringRef] : (owner[stringRef] = value);
781+
if (!disableStringRefs) {
782+
if (!owner)
783+
throw Error(
784+
"Element ref was specified as a string (" +
785+
stringRef +
786+
") but no owner was set. This could happen for one of the following reasons:\n1. You may be adding a ref to a function component\n2. You may be adding a ref to a component that was not created inside a component's render method\n3. You have multiple copies of React loaded\nSee https://react.dev/link/refs-must-have-owner for more information."
787+
);
788+
if (1 !== owner.tag)
789+
throw Error(
790+
"Function components cannot have string refs. We recommend using useRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref"
791+
);
792+
if (
793+
"function" !== typeof type ||
794+
(type.prototype && type.prototype.isReactComponent)
795+
)
796+
(type = getComponentNameFromFiber(owner) || "Component"),
797+
didWarnAboutStringRefs[type] ||
798+
(enableLogStringRefsProd &&
799+
enableLogStringRefsProd(type, stringRef),
800+
error(
801+
'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://react.dev/link/strict-mode-string-ref',
802+
type,
803+
stringRef
804+
),
805+
(didWarnAboutStringRefs[type] = !0));
806+
owner = owner.stateNode;
807+
if (!owner)
808+
throw Error(
809+
"Missing owner for string ref " +
810+
stringRef +
811+
". This error is likely caused by a bug in React. Please file an issue."
812+
);
813+
owner = owner.refs;
814+
null === value ? delete owner[stringRef] : (owner[stringRef] = value);
815+
}
807816
}
808817
var React = require("react"),
809818
dynamicFeatureFlags = require("ReactFeatureFlags"),
810819
disableDefaultPropsExceptForClasses =
811820
dynamicFeatureFlags.disableDefaultPropsExceptForClasses,
821+
disableStringRefs = dynamicFeatureFlags.disableStringRefs,
812822
enableDebugTracing = dynamicFeatureFlags.enableDebugTracing,
813823
enableLogStringRefsProd = dynamicFeatureFlags.enableLogStringRefsProd,
814824
enableRenderableContext = dynamicFeatureFlags.enableRenderableContext,
@@ -863,7 +873,8 @@ __DEV__ &&
863873
specialPropKeyWarningShown;
864874
var didWarnAboutStringRefs = {};
865875
var didWarnAboutElementRef = {};
866-
var didWarnAboutKeySpread = {},
876+
var enableFastJSXWithoutStringRefs = disableStringRefs,
877+
didWarnAboutKeySpread = {},
867878
ownerHasKeyUseWarning = {};
868879
exports.Fragment = REACT_FRAGMENT_TYPE;
869880
exports.jsxDEV = function (

compiled/facebook-www/REVISION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5636fad840942cfea80301d91e931a50c6370d19
1+
75dd053b5e83e8ae20e9f771bca7b95dba4ff881
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5636fad840942cfea80301d91e931a50c6370d19
1+
75dd053b5e83e8ae20e9f771bca7b95dba4ff881

0 commit comments

Comments
 (0)