From b6896fe7155567571fcf9290fa12ff439e6b7baf Mon Sep 17 00:00:00 2001 From: ErfanMBE Date: Tue, 30 Dec 2025 15:37:16 +0330 Subject: [PATCH 1/2] Deduplicate DEV warning for missing effect callbacks --- README.md | 4 +++ packages/react/src/ReactHooks.js | 54 ++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 88f8e827b02..ba94448b4af 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,10 @@ The documentation is divided into several sections: You can improve it by sending pull requests to [this repository](https://github.com/reactjs/react.dev). +## Learning this Repository + +If you want to learn React by reading this codebase, start with docs/LEARNING_PLAN.md for a structured, hands‑on path through the packages, build scripts, fixtures, and the React Compiler. + ## Examples We have several examples [on the website](https://react.dev/). Here is the first one to get you started: diff --git a/packages/react/src/ReactHooks.js b/packages/react/src/ReactHooks.js index ff86130baa0..23a50d6e314 100644 --- a/packages/react/src/ReactHooks.js +++ b/packages/react/src/ReactHooks.js @@ -41,6 +41,33 @@ function resolveDispatcher() { return ((dispatcher: any): Dispatcher); } +function warnOnMissingEffectCallback( + hookName: 'useEffect' | 'useInsertionEffect' | 'useLayoutEffect', + create: mixed, +): void { + if (__DEV__) { + if (create == null) { + switch (hookName) { + case 'useEffect': + console.warn( + 'React Hook useEffect requires an effect callback. Did you forget to pass a callback to the hook?', + ); + break; + case 'useInsertionEffect': + console.warn( + 'React Hook useInsertionEffect requires an effect callback. Did you forget to pass a callback to the hook?', + ); + break; + case 'useLayoutEffect': + console.warn( + 'React Hook useLayoutEffect requires an effect callback. Did you forget to pass a callback to the hook?', + ); + break; + } + } + } +} + export function getCacheForType(resourceType: () => T): T { const dispatcher = ReactSharedInternals.A; if (!dispatcher) { @@ -88,14 +115,7 @@ export function useEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { - if (__DEV__) { - if (create == null) { - console.warn( - 'React Hook useEffect requires an effect callback. Did you forget to pass a callback to the hook?', - ); - } - } - + warnOnMissingEffectCallback('useEffect', create); const dispatcher = resolveDispatcher(); return dispatcher.useEffect(create, deps); } @@ -104,14 +124,7 @@ export function useInsertionEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { - if (__DEV__) { - if (create == null) { - console.warn( - 'React Hook useInsertionEffect requires an effect callback. Did you forget to pass a callback to the hook?', - ); - } - } - + warnOnMissingEffectCallback('useInsertionEffect', create); const dispatcher = resolveDispatcher(); return dispatcher.useInsertionEffect(create, deps); } @@ -120,14 +133,7 @@ export function useLayoutEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { - if (__DEV__) { - if (create == null) { - console.warn( - 'React Hook useLayoutEffect requires an effect callback. Did you forget to pass a callback to the hook?', - ); - } - } - + warnOnMissingEffectCallback('useLayoutEffect', create); const dispatcher = resolveDispatcher(); return dispatcher.useLayoutEffect(create, deps); } From 6388cb195de4bb42fb4391e09cb57ab9e458c9a5 Mon Sep 17 00:00:00 2001 From: ErfanMBE Date: Tue, 30 Dec 2025 15:40:06 +0330 Subject: [PATCH 2/2] Deduplicate DEV warning for missing effect callbacks --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index ba94448b4af..88f8e827b02 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,6 @@ The documentation is divided into several sections: You can improve it by sending pull requests to [this repository](https://github.com/reactjs/react.dev). -## Learning this Repository - -If you want to learn React by reading this codebase, start with docs/LEARNING_PLAN.md for a structured, hands‑on path through the packages, build scripts, fixtures, and the React Compiler. - ## Examples We have several examples [on the website](https://react.dev/). Here is the first one to get you started: