From 7c6fc572165a7d3d2db30b32fa41bd61b6e5aa24 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 11 Feb 2025 07:48:12 -0500 Subject: [PATCH 1/3] Added dev-only warning for null/undefined create in use*Effect --- packages/react/src/ReactHooks.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/react/src/ReactHooks.js b/packages/react/src/ReactHooks.js index 06d61d223874..bbd24fec05f1 100644 --- a/packages/react/src/ReactHooks.js +++ b/packages/react/src/ReactHooks.js @@ -93,6 +93,12 @@ export function useEffect( updateDeps?: Array | void | null, destroy?: ((resource: {...} | void | null) => void) | void, ): void { + if (__DEV__ && 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 +124,12 @@ export function useInsertionEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { + if (__DEV__ && 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 +138,12 @@ export function useLayoutEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { + if (__DEV__ && 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); } From 45e4ca4baf3bbb25cff27a6214fada3f47c26601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Tue, 11 Feb 2025 16:09:11 -0500 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: lauren --- packages/react/src/ReactHooks.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/react/src/ReactHooks.js b/packages/react/src/ReactHooks.js index bbd24fec05f1..d483016e3024 100644 --- a/packages/react/src/ReactHooks.js +++ b/packages/react/src/ReactHooks.js @@ -93,10 +93,12 @@ export function useEffect( updateDeps?: Array | void | null, destroy?: ((resource: {...} | void | null) => void) | void, ): void { - if (__DEV__ && create == null) { - console.warn( - 'React Hook useEffect requires an effect callback. Did you forget to pass a callback to the hook?', - ); + 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(); From 3d2665735d2e3d139e242f9a678359bf19aca6a0 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 11 Feb 2025 16:10:28 -0500 Subject: [PATCH 3/3] refactor: inline the other create == null checks --- packages/react/src/ReactHooks.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/react/src/ReactHooks.js b/packages/react/src/ReactHooks.js index d483016e3024..ba6b0ffbcb71 100644 --- a/packages/react/src/ReactHooks.js +++ b/packages/react/src/ReactHooks.js @@ -93,7 +93,7 @@ export function useEffect( updateDeps?: Array | void | null, destroy?: ((resource: {...} | void | null) => void) | void, ): void { - if (__DEV__ ) { + if (__DEV__) { if (create == null) { console.warn( 'React Hook useEffect requires an effect callback. Did you forget to pass a callback to the hook?', @@ -126,10 +126,12 @@ export function useInsertionEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { - if (__DEV__ && create == null) { - console.warn( - 'React Hook useInsertionEffect requires an effect callback. Did you forget to pass a callback to the hook?', - ); + 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(); @@ -140,10 +142,12 @@ export function useLayoutEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { - if (__DEV__ && create == null) { - console.warn( - 'React Hook useLayoutEffect requires an effect callback. Did you forget to pass a callback to the hook?', - ); + 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();