From e0d3b49cb9d012722976a59e7cb16e945232cfab Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 20 Jan 2022 15:18:38 +0000 Subject: [PATCH] Warn when a callback ref returns a function (#23145) --- packages/react-dom/src/__tests__/refs-test.js | 1 - .../react-reconciler/src/ReactFiberCommitWork.new.js | 11 ++--------- .../react-reconciler/src/ReactFiberCommitWork.old.js | 11 ++--------- packages/shared/ReactFeatureFlags.js | 2 -- packages/shared/forks/ReactFeatureFlags.native-fb.js | 1 - packages/shared/forks/ReactFeatureFlags.native-oss.js | 1 - .../shared/forks/ReactFeatureFlags.test-renderer.js | 1 - .../forks/ReactFeatureFlags.test-renderer.native.js | 1 - .../forks/ReactFeatureFlags.test-renderer.www.js | 1 - packages/shared/forks/ReactFeatureFlags.testing.js | 1 - .../shared/forks/ReactFeatureFlags.testing.www.js | 1 - .../shared/forks/ReactFeatureFlags.www-dynamic.js | 1 - packages/shared/forks/ReactFeatureFlags.www.js | 1 - 13 files changed, 4 insertions(+), 30 deletions(-) diff --git a/packages/react-dom/src/__tests__/refs-test.js b/packages/react-dom/src/__tests__/refs-test.js index 872ff6e7dc2b6..8e3643d2c75d0 100644 --- a/packages/react-dom/src/__tests__/refs-test.js +++ b/packages/react-dom/src/__tests__/refs-test.js @@ -352,7 +352,6 @@ describe('ref swapping', () => { ); }); - // @gate !__DEV__ || warnAboutCallbackRefReturningFunction it('should warn about callback refs returning a function', () => { const container = document.createElement('div'); expect(() => { diff --git a/packages/react-reconciler/src/ReactFiberCommitWork.new.js b/packages/react-reconciler/src/ReactFiberCommitWork.new.js index ecb83e28171c2..5ff6830096687 100644 --- a/packages/react-reconciler/src/ReactFiberCommitWork.new.js +++ b/packages/react-reconciler/src/ReactFiberCommitWork.new.js @@ -39,7 +39,6 @@ import { deletedTreeCleanUpLevel, enableSuspenseLayoutEffectSemantics, enableUpdaterTracking, - warnAboutCallbackRefReturningFunction, enableCache, } from 'shared/ReactFeatureFlags'; import { @@ -286,10 +285,7 @@ function safelyDetachRef(current: Fiber, nearestMountedAncestor: Fiber | null) { captureCommitPhaseError(current, nearestMountedAncestor, error); } if (__DEV__) { - if ( - warnAboutCallbackRefReturningFunction && - typeof retVal === 'function' - ) { + if (typeof retVal === 'function') { console.error( 'Unexpected return value from a callback ref in %s. ' + 'A callback ref should not return a function.', @@ -1151,10 +1147,7 @@ function commitAttachRef(finishedWork: Fiber) { retVal = ref(instanceToUse); } if (__DEV__) { - if ( - warnAboutCallbackRefReturningFunction && - typeof retVal === 'function' - ) { + if (typeof retVal === 'function') { console.error( 'Unexpected return value from a callback ref in %s. ' + 'A callback ref should not return a function.', diff --git a/packages/react-reconciler/src/ReactFiberCommitWork.old.js b/packages/react-reconciler/src/ReactFiberCommitWork.old.js index 8466b41c85dcf..769974e048d51 100644 --- a/packages/react-reconciler/src/ReactFiberCommitWork.old.js +++ b/packages/react-reconciler/src/ReactFiberCommitWork.old.js @@ -39,7 +39,6 @@ import { deletedTreeCleanUpLevel, enableSuspenseLayoutEffectSemantics, enableUpdaterTracking, - warnAboutCallbackRefReturningFunction, enableCache, } from 'shared/ReactFeatureFlags'; import { @@ -286,10 +285,7 @@ function safelyDetachRef(current: Fiber, nearestMountedAncestor: Fiber | null) { captureCommitPhaseError(current, nearestMountedAncestor, error); } if (__DEV__) { - if ( - warnAboutCallbackRefReturningFunction && - typeof retVal === 'function' - ) { + if (typeof retVal === 'function') { console.error( 'Unexpected return value from a callback ref in %s. ' + 'A callback ref should not return a function.', @@ -1151,10 +1147,7 @@ function commitAttachRef(finishedWork: Fiber) { retVal = ref(instanceToUse); } if (__DEV__) { - if ( - warnAboutCallbackRefReturningFunction && - typeof retVal === 'function' - ) { + if (typeof retVal === 'function') { console.error( 'Unexpected return value from a callback ref in %s. ' + 'A callback ref should not return a function.', diff --git a/packages/shared/ReactFeatureFlags.js b/packages/shared/ReactFeatureFlags.js index 7800bb1a7e3bd..3c2444101eb87 100644 --- a/packages/shared/ReactFeatureFlags.js +++ b/packages/shared/ReactFeatureFlags.js @@ -178,8 +178,6 @@ export const deferRenderPhaseUpdateToNextBatch = false; export const enableUseRefAccessWarning = false; -export const warnAboutCallbackRefReturningFunction = false; - export const disableSchedulerTimeoutInWorkLoop = false; export const enableLazyContextPropagation = false; diff --git a/packages/shared/forks/ReactFeatureFlags.native-fb.js b/packages/shared/forks/ReactFeatureFlags.native-fb.js index 65b1549876bb7..2e2c637087809 100644 --- a/packages/shared/forks/ReactFeatureFlags.native-fb.js +++ b/packages/shared/forks/ReactFeatureFlags.native-fb.js @@ -66,7 +66,6 @@ export const deferRenderPhaseUpdateToNextBatch = false; export const enableStrictEffects = __DEV__; export const createRootStrictEffectsByDefault = false; export const enableUseRefAccessWarning = false; -export const warnAboutCallbackRefReturningFunction = false; export const disableSchedulerTimeoutInWorkLoop = false; export const enableLazyContextPropagation = false; diff --git a/packages/shared/forks/ReactFeatureFlags.native-oss.js b/packages/shared/forks/ReactFeatureFlags.native-oss.js index 34bdac6ac2c6e..b97cd2317f0ba 100644 --- a/packages/shared/forks/ReactFeatureFlags.native-oss.js +++ b/packages/shared/forks/ReactFeatureFlags.native-oss.js @@ -57,7 +57,6 @@ export const deferRenderPhaseUpdateToNextBatch = false; export const enableStrictEffects = false; export const createRootStrictEffectsByDefault = false; export const enableUseRefAccessWarning = false; -export const warnAboutCallbackRefReturningFunction = false; export const disableSchedulerTimeoutInWorkLoop = false; export const enableLazyContextPropagation = false; diff --git a/packages/shared/forks/ReactFeatureFlags.test-renderer.js b/packages/shared/forks/ReactFeatureFlags.test-renderer.js index c739a1e12620c..b0e3fc3a0c8bd 100644 --- a/packages/shared/forks/ReactFeatureFlags.test-renderer.js +++ b/packages/shared/forks/ReactFeatureFlags.test-renderer.js @@ -57,7 +57,6 @@ export const deferRenderPhaseUpdateToNextBatch = false; export const enableStrictEffects = false; export const createRootStrictEffectsByDefault = false; export const enableUseRefAccessWarning = false; -export const warnAboutCallbackRefReturningFunction = false; export const disableSchedulerTimeoutInWorkLoop = false; export const enableLazyContextPropagation = false; diff --git a/packages/shared/forks/ReactFeatureFlags.test-renderer.native.js b/packages/shared/forks/ReactFeatureFlags.test-renderer.native.js index cd72fcc481c9d..953fab0f2c0ad 100644 --- a/packages/shared/forks/ReactFeatureFlags.test-renderer.native.js +++ b/packages/shared/forks/ReactFeatureFlags.test-renderer.native.js @@ -56,7 +56,6 @@ export const enableClientRenderFallbackOnHydrationMismatch = true; export const enableStrictEffects = false; export const createRootStrictEffectsByDefault = false; export const enableUseRefAccessWarning = false; -export const warnAboutCallbackRefReturningFunction = false; export const disableSchedulerTimeoutInWorkLoop = false; export const enableLazyContextPropagation = false; diff --git a/packages/shared/forks/ReactFeatureFlags.test-renderer.www.js b/packages/shared/forks/ReactFeatureFlags.test-renderer.www.js index 2e35505f58888..e9e3b5e97b64b 100644 --- a/packages/shared/forks/ReactFeatureFlags.test-renderer.www.js +++ b/packages/shared/forks/ReactFeatureFlags.test-renderer.www.js @@ -57,7 +57,6 @@ export const deferRenderPhaseUpdateToNextBatch = false; export const enableStrictEffects = true; export const createRootStrictEffectsByDefault = false; export const enableUseRefAccessWarning = false; -export const warnAboutCallbackRefReturningFunction = false; export const disableSchedulerTimeoutInWorkLoop = false; export const enableLazyContextPropagation = false; diff --git a/packages/shared/forks/ReactFeatureFlags.testing.js b/packages/shared/forks/ReactFeatureFlags.testing.js index 1d2ff85dd3899..ae8d16d41c000 100644 --- a/packages/shared/forks/ReactFeatureFlags.testing.js +++ b/packages/shared/forks/ReactFeatureFlags.testing.js @@ -57,7 +57,6 @@ export const deferRenderPhaseUpdateToNextBatch = false; export const enableStrictEffects = false; export const createRootStrictEffectsByDefault = false; export const enableUseRefAccessWarning = false; -export const warnAboutCallbackRefReturningFunction = false; export const disableSchedulerTimeoutInWorkLoop = false; export const enableLazyContextPropagation = false; diff --git a/packages/shared/forks/ReactFeatureFlags.testing.www.js b/packages/shared/forks/ReactFeatureFlags.testing.www.js index 06834fa9dbaa8..93bfcfb5a70cf 100644 --- a/packages/shared/forks/ReactFeatureFlags.testing.www.js +++ b/packages/shared/forks/ReactFeatureFlags.testing.www.js @@ -57,7 +57,6 @@ export const deferRenderPhaseUpdateToNextBatch = false; export const enableStrictEffects = false; export const createRootStrictEffectsByDefault = false; export const enableUseRefAccessWarning = false; -export const warnAboutCallbackRefReturningFunction = false; export const disableSchedulerTimeoutInWorkLoop = false; export const enableLazyContextPropagation = false; diff --git a/packages/shared/forks/ReactFeatureFlags.www-dynamic.js b/packages/shared/forks/ReactFeatureFlags.www-dynamic.js index 288e9631c75e1..ab5e1526e3e67 100644 --- a/packages/shared/forks/ReactFeatureFlags.www-dynamic.js +++ b/packages/shared/forks/ReactFeatureFlags.www-dynamic.js @@ -19,7 +19,6 @@ export const enableFilterEmptyStringAttributesDOM = __VARIANT__; export const enableLegacyFBSupport = __VARIANT__; export const skipUnmountedBoundaries = __VARIANT__; export const enableUseRefAccessWarning = __VARIANT__; -export const warnAboutCallbackRefReturningFunction = __VARIANT__; export const deletedTreeCleanUpLevel = __VARIANT__ ? 3 : 1; export const enableProfilerNestedUpdateScheduledHook = __VARIANT__; export const disableSchedulerTimeoutInWorkLoop = __VARIANT__; diff --git a/packages/shared/forks/ReactFeatureFlags.www.js b/packages/shared/forks/ReactFeatureFlags.www.js index a666671ab0acd..c15550727c6e8 100644 --- a/packages/shared/forks/ReactFeatureFlags.www.js +++ b/packages/shared/forks/ReactFeatureFlags.www.js @@ -67,7 +67,6 @@ export const warnAboutDefaultPropsOnFunctionComponents = false; export const enableGetInspectorDataForInstanceInProduction = false; export const enableSuspenseServerRenderer = true; export const enableSelectiveHydration = true; -export const warnAboutCallbackRefReturningFunction = true; export const enableLazyElements = true; export const enableCache = true;