Skip to content

Commit 9b55fcc

Browse files
committed
[string-refs] log string ref from prod
If passed as a feature flag, this calls the configured function when a string ref is used even from prod code to find the last usages.
1 parent de43d56 commit 9b55fcc

9 files changed

+47
-18
lines changed

β€Žpackages/react/src/jsx/ReactJSXElement.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
disableStringRefs,
2525
disableDefaultPropsExceptForClasses,
2626
enableOwnerStacks,
27+
logStringRefs,
2728
} from 'shared/ReactFeatureFlags';
2829
import {checkPropStringCoercion} from 'shared/CheckStringCoercion';
2930
import {ClassComponent} from 'react-reconciler/src/ReactWorkTags';
@@ -76,7 +77,7 @@ let didWarnAboutStringRefs;
7677
let didWarnAboutElementRef;
7778
let didWarnAboutOldJSXRuntime;
7879

79-
if (__DEV__) {
80+
if (__DEV__ || logStringRefs) {
8081
didWarnAboutStringRefs = {};
8182
didWarnAboutElementRef = {};
8283
}
@@ -1314,22 +1315,27 @@ function stringRefAsCallbackRef(stringRef, type, owner, value) {
13141315
);
13151316
}
13161317

1317-
if (__DEV__) {
1318+
if (__DEV__ || logStringRefs) {
13181319
if (
13191320
// Will already warn with "Function components cannot be given refs"
13201321
!(typeof type === 'function' && !isReactClass(type))
13211322
) {
13221323
const componentName = getComponentNameFromFiber(owner) || 'Component';
13231324
if (!didWarnAboutStringRefs[componentName]) {
1324-
console.error(
1325-
'Component "%s" contains the string ref "%s". Support for string refs ' +
1326-
'will be removed in a future major release. We recommend using ' +
1327-
'useRef() or createRef() instead. ' +
1328-
'Learn more about using refs safely here: ' +
1329-
'https://react.dev/link/strict-mode-string-ref',
1330-
componentName,
1331-
stringRef,
1332-
);
1325+
if (logStringRefs) {
1326+
logStringRefs(componentName, stringRef);
1327+
}
1328+
if (__DEV__) {
1329+
console.error(
1330+
'Component "%s" contains the string ref "%s". Support for string refs ' +
1331+
'will be removed in a future major release. We recommend using ' +
1332+
'useRef() or createRef() instead. ' +
1333+
'Learn more about using refs safely here: ' +
1334+
'https://react.dev/link/strict-mode-string-ref',
1335+
componentName,
1336+
stringRef,
1337+
);
1338+
}
13331339
didWarnAboutStringRefs[componentName] = true;
13341340
}
13351341
}

β€Žpackages/shared/ReactFeatureFlags.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ export const disableClientCache = true;
215215
// during element creation.
216216
export const enableRefAsProp = true;
217217
export const disableStringRefs = true;
218+
export const logStringRefs: null | ((string, string) => void) = null;
218219

219220
// Warn on any usage of ReactTestRenderer
220221
export const enableReactTestRendererWarning = true;

β€Žpackages/shared/forks/ReactFeatureFlags.native-fb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export const enableUpdaterTracking = __PROFILE__;
8989
export const enableUseEffectEventHook = false;
9090
export const enableUseMemoCacheHook = true;
9191
export const favorSafetyOverHydrationPerf = true;
92+
export const logStringRefs: null | ((string, string) => void) = null;
9293
export const renameElementSymbol = false;
9394
export const retryLaneExpirationMs = 5000;
9495
export const syncLaneExpirationMs = 250;

β€Žpackages/shared/forks/ReactFeatureFlags.native-oss.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const enableTrustedTypesIntegration = false;
7979
export const enableUseEffectEventHook = false;
8080
export const enableUseMemoCacheHook = true;
8181
export const favorSafetyOverHydrationPerf = true;
82+
export const logStringRefs: null | ((string, string) => void) = null;
8283
export const passChildrenWhenCloningPersistedNodes = false;
8384
export const renameElementSymbol = true;
8485
export const retryLaneExpirationMs = 5000;

β€Žpackages/shared/forks/ReactFeatureFlags.test-renderer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const enableFilterEmptyStringAttributesDOM = true;
4848
export const enableGetInspectorDataForInstanceInProduction = false;
4949
export const enableFabricCompleteRootInCommitPhase = false;
5050
export const enableHiddenSubtreeInsertionEffectCleanup = false;
51+
export const logStringRefs: null | ((string, string) => void) = null;
5152

5253
export const enableRetryLaneExpiration = false;
5354
export const retryLaneExpirationMs = 5000;

β€Žpackages/shared/forks/ReactFeatureFlags.test-renderer.www.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const enableGetInspectorDataForInstanceInProduction = false;
5151
export const enableRenderableContext = false;
5252
export const enableFabricCompleteRootInCommitPhase = false;
5353
export const enableHiddenSubtreeInsertionEffectCleanup = true;
54+
export const logStringRefs: null | ((string, string) => void) = null;
5455

5556
export const enableRetryLaneExpiration = false;
5657
export const retryLaneExpirationMs = 5000;

β€Žpackages/shared/forks/ReactFeatureFlags.www-dynamic.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const renameElementSymbol = __VARIANT__;
3131
export const retryLaneExpirationMs = 5000;
3232
export const syncLaneExpirationMs = 250;
3333
export const transitionLaneExpirationMs = 5000;
34+
export const logStringRefs: null | ((string, string) => void) = null;
3435

3536
// Enable this flag to help with concurrent mode debugging.
3637
// It logs information to the console about React scheduling, rendering, and commit phases.

β€Žpackages/shared/forks/ReactFeatureFlags.www.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const {
3636
syncLaneExpirationMs,
3737
transitionLaneExpirationMs,
3838
enableSiblingPrerendering,
39+
logStringRefs,
3940
} = dynamicFeatureFlags;
4041

4142
// On WWW, __EXPERIMENTAL__ is used for a new modern build.

β€Žscripts/flags/flags.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function getNextMajorFlagValue(flag) {
172172
const value = ReactFeatureFlagsMajor[flag];
173173
if (value === true || value === 'next') {
174174
return 'βœ…';
175-
} else if (value === false || value === 'experimental') {
175+
} else if (value === false || value === null || value === 'experimental') {
176176
return '❌';
177177
} else if (value === 'profile') {
178178
return 'πŸ“Š';
@@ -189,7 +189,12 @@ function getOSSCanaryFlagValue(flag) {
189189
const value = ReactFeatureFlags[flag];
190190
if (value === true) {
191191
return 'βœ…';
192-
} else if (value === false || value === 'experimental' || value === 'next') {
192+
} else if (
193+
value === false ||
194+
value === null ||
195+
value === 'experimental' ||
196+
value === 'next'
197+
) {
193198
return '❌';
194199
} else if (value === 'profile') {
195200
return 'πŸ“Š';
@@ -206,7 +211,7 @@ function getOSSExperimentalFlagValue(flag) {
206211
const value = ReactFeatureFlags[flag];
207212
if (value === true || value === 'experimental') {
208213
return 'βœ…';
209-
} else if (value === false || value === 'next') {
214+
} else if (value === false || value === null || value === 'next') {
210215
return '❌';
211216
} else if (value === 'profile') {
212217
return 'πŸ“Š';
@@ -225,7 +230,7 @@ function getWWWModernFlagValue(flag) {
225230
const value = ReactFeatureFlagsWWW[flag];
226231
if (value === true || value === 'experimental') {
227232
return 'βœ…';
228-
} else if (value === false || value === 'next') {
233+
} else if (value === false || value === null || value === 'next') {
229234
return '❌';
230235
} else if (value === 'profile') {
231236
return 'πŸ“Š';
@@ -244,7 +249,12 @@ function getWWWClassicFlagValue(flag) {
244249
const value = ReactFeatureFlagsWWW[flag];
245250
if (value === true) {
246251
return 'βœ…';
247-
} else if (value === false || value === 'experimental' || value === 'next') {
252+
} else if (
253+
value === false ||
254+
value === null ||
255+
value === 'experimental' ||
256+
value === 'next'
257+
) {
248258
return '❌';
249259
} else if (value === 'profile') {
250260
return 'πŸ“Š';
@@ -265,7 +275,7 @@ function getRNNextMajorFlagValue(flag) {
265275
return 'βœ…';
266276
} else if (value === 'next-todo') {
267277
return 'πŸ“‹';
268-
} else if (value === false || value === 'experimental') {
278+
} else if (value === false || value === null || value === 'experimental') {
269279
return '❌';
270280
} else if (value === 'profile') {
271281
return 'πŸ“Š';
@@ -286,6 +296,7 @@ function getRNOSSFlagValue(flag) {
286296
return 'βœ…';
287297
} else if (
288298
value === false ||
299+
value === null ||
289300
value === 'experimental' ||
290301
value === 'next' ||
291302
value === 'next-todo'
@@ -308,7 +319,12 @@ function getRNFBFlagValue(flag) {
308319
const value = ReactFeatureFlagsNativeFB[flag];
309320
if (value === true) {
310321
return 'βœ…';
311-
} else if (value === false || value === 'experimental' || value === 'next') {
322+
} else if (
323+
value === false ||
324+
value === null ||
325+
value === 'experimental' ||
326+
value === 'next'
327+
) {
312328
return '❌';
313329
} else if (value === 'profile') {
314330
return 'πŸ“Š';

0 commit comments

Comments
Β (0)