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 d5bba18 commit e5f148d
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 @@ -217,6 +217,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 @@ -90,6 +90,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 @@ -80,6 +80,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 @@ -32,6 +32,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 @@ -37,6 +37,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 e5f148d

Please sign in to comment.