From 192555bb0ed88db30f91c58651c421f178f90384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Tue, 11 Feb 2025 17:01:04 -0500 Subject: [PATCH] Added dev-only warning for null/undefined create in use*Effect (#32355) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Fixes #32354. Re-creation of #15197: adds a dev-only warning if `create == null` to the three `use*Effect` functions: * `useEffect` * `useInsertionEffect` * `useLayoutEffect` Updates the warning to match the same text given in the `react/exhaustive-deps` lint rule. ## How did you test this change? I applied the changes manually within `node_modules/` on a local clone of https://github.com/JoshuaKGoldberg/repros/tree/react-use-effect-no-arguments. Please pardon me for opening a PR addressing a not-accepted issue. I was excited to get back to #15194 -> #15197 now that I have time. 🙂 --------- Co-authored-by: lauren --- packages/react/src/ReactHooks.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/react/src/ReactHooks.js b/packages/react/src/ReactHooks.js index 06d61d2238..ba6b0ffbcb 100644 --- a/packages/react/src/ReactHooks.js +++ b/packages/react/src/ReactHooks.js @@ -93,6 +93,14 @@ export function useEffect( updateDeps?: Array | void | null, destroy?: ((resource: {...} | void | null) => void) | void, ): 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?', + ); + } + } + const dispatcher = resolveDispatcher(); if ( enableUseEffectCRUDOverload && @@ -118,6 +126,14 @@ 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?', + ); + } + } + const dispatcher = resolveDispatcher(); return dispatcher.useInsertionEffect(create, deps); } @@ -126,6 +142,14 @@ 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?', + ); + } + } + const dispatcher = resolveDispatcher(); return dispatcher.useLayoutEffect(create, deps); }