Skip to content

Commit

Permalink
[string-refs] log string ref from prod
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kassens committed Oct 9, 2024
1 parent de43d56 commit 9b55fcc
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 18 deletions.
28 changes: 17 additions & 11 deletions packages/react/src/jsx/ReactJSXElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
disableStringRefs,
disableDefaultPropsExceptForClasses,
enableOwnerStacks,
logStringRefs,
} from 'shared/ReactFeatureFlags';
import {checkPropStringCoercion} from 'shared/CheckStringCoercion';
import {ClassComponent} from 'react-reconciler/src/ReactWorkTags';
Expand Down Expand Up @@ -76,7 +77,7 @@ let didWarnAboutStringRefs;
let didWarnAboutElementRef;
let didWarnAboutOldJSXRuntime;

if (__DEV__) {
if (__DEV__ || logStringRefs) {
didWarnAboutStringRefs = {};
didWarnAboutElementRef = {};
}
Expand Down Expand Up @@ -1314,22 +1315,27 @@ function stringRefAsCallbackRef(stringRef, type, owner, value) {
);
}

if (__DEV__) {
if (__DEV__ || logStringRefs) {
if (
// Will already warn with "Function components cannot be given refs"
!(typeof type === 'function' && !isReactClass(type))
) {
const componentName = getComponentNameFromFiber(owner) || 'Component';
if (!didWarnAboutStringRefs[componentName]) {
console.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://react.dev/link/strict-mode-string-ref',
componentName,
stringRef,
);
if (logStringRefs) {
logStringRefs(componentName, stringRef);
}
if (__DEV__) {
console.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://react.dev/link/strict-mode-string-ref',
componentName,
stringRef,
);
}
didWarnAboutStringRefs[componentName] = true;
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/shared/ReactFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export const disableClientCache = true;
// during element creation.
export const enableRefAsProp = true;
export const disableStringRefs = true;
export const logStringRefs: null | ((string, string) => void) = null;

// Warn on any usage of ReactTestRenderer
export const enableReactTestRendererWarning = true;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.native-fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const enableUpdaterTracking = __PROFILE__;
export const enableUseEffectEventHook = false;
export const enableUseMemoCacheHook = true;
export const favorSafetyOverHydrationPerf = true;
export const logStringRefs: null | ((string, string) => void) = null;
export const renameElementSymbol = false;
export const retryLaneExpirationMs = 5000;
export const syncLaneExpirationMs = 250;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.native-oss.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const enableTrustedTypesIntegration = false;
export const enableUseEffectEventHook = false;
export const enableUseMemoCacheHook = true;
export const favorSafetyOverHydrationPerf = true;
export const logStringRefs: null | ((string, string) => void) = null;
export const passChildrenWhenCloningPersistedNodes = false;
export const renameElementSymbol = true;
export const retryLaneExpirationMs = 5000;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.test-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const enableFilterEmptyStringAttributesDOM = true;
export const enableGetInspectorDataForInstanceInProduction = false;
export const enableFabricCompleteRootInCommitPhase = false;
export const enableHiddenSubtreeInsertionEffectCleanup = false;
export const logStringRefs: null | ((string, string) => void) = null;

export const enableRetryLaneExpiration = false;
export const retryLaneExpirationMs = 5000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const enableGetInspectorDataForInstanceInProduction = false;
export const enableRenderableContext = false;
export const enableFabricCompleteRootInCommitPhase = false;
export const enableHiddenSubtreeInsertionEffectCleanup = true;
export const logStringRefs: null | ((string, string) => void) = null;

export const enableRetryLaneExpiration = false;
export const retryLaneExpirationMs = 5000;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.www-dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const renameElementSymbol = __VARIANT__;
export const retryLaneExpirationMs = 5000;
export const syncLaneExpirationMs = 250;
export const transitionLaneExpirationMs = 5000;
export const logStringRefs: null | ((string, string) => void) = null;

// Enable this flag to help with concurrent mode debugging.
// It logs information to the console about React scheduling, rendering, and commit phases.
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.www.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const {
syncLaneExpirationMs,
transitionLaneExpirationMs,
enableSiblingPrerendering,
logStringRefs,
} = dynamicFeatureFlags;

// On WWW, __EXPERIMENTAL__ is used for a new modern build.
Expand Down
30 changes: 23 additions & 7 deletions scripts/flags/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function getNextMajorFlagValue(flag) {
const value = ReactFeatureFlagsMajor[flag];
if (value === true || value === 'next') {
return '✅';
} else if (value === false || value === 'experimental') {
} else if (value === false || value === null || value === 'experimental') {
return '❌';
} else if (value === 'profile') {
return '📊';
Expand All @@ -189,7 +189,12 @@ function getOSSCanaryFlagValue(flag) {
const value = ReactFeatureFlags[flag];
if (value === true) {
return '✅';
} else if (value === false || value === 'experimental' || value === 'next') {
} else if (
value === false ||
value === null ||
value === 'experimental' ||
value === 'next'
) {
return '❌';
} else if (value === 'profile') {
return '📊';
Expand All @@ -206,7 +211,7 @@ function getOSSExperimentalFlagValue(flag) {
const value = ReactFeatureFlags[flag];
if (value === true || value === 'experimental') {
return '✅';
} else if (value === false || value === 'next') {
} else if (value === false || value === null || value === 'next') {
return '❌';
} else if (value === 'profile') {
return '📊';
Expand All @@ -225,7 +230,7 @@ function getWWWModernFlagValue(flag) {
const value = ReactFeatureFlagsWWW[flag];
if (value === true || value === 'experimental') {
return '✅';
} else if (value === false || value === 'next') {
} else if (value === false || value === null || value === 'next') {
return '❌';
} else if (value === 'profile') {
return '📊';
Expand All @@ -244,7 +249,12 @@ function getWWWClassicFlagValue(flag) {
const value = ReactFeatureFlagsWWW[flag];
if (value === true) {
return '✅';
} else if (value === false || value === 'experimental' || value === 'next') {
} else if (
value === false ||
value === null ||
value === 'experimental' ||
value === 'next'
) {
return '❌';
} else if (value === 'profile') {
return '📊';
Expand All @@ -265,7 +275,7 @@ function getRNNextMajorFlagValue(flag) {
return '✅';
} else if (value === 'next-todo') {
return '📋';
} else if (value === false || value === 'experimental') {
} else if (value === false || value === null || value === 'experimental') {
return '❌';
} else if (value === 'profile') {
return '📊';
Expand All @@ -286,6 +296,7 @@ function getRNOSSFlagValue(flag) {
return '✅';
} else if (
value === false ||
value === null ||
value === 'experimental' ||
value === 'next' ||
value === 'next-todo'
Expand All @@ -308,7 +319,12 @@ function getRNFBFlagValue(flag) {
const value = ReactFeatureFlagsNativeFB[flag];
if (value === true) {
return '✅';
} else if (value === false || value === 'experimental' || value === 'next') {
} else if (
value === false ||
value === null ||
value === 'experimental' ||
value === 'next'
) {
return '❌';
} else if (value === 'profile') {
return '📊';
Expand Down

0 comments on commit 9b55fcc

Please sign in to comment.