Skip to content

Commit

Permalink
Added dev-only warning for null/undefined create in use*Effect (#32355)
Browse files Browse the repository at this point in the history
## 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 <poteto@users.noreply.github.com>
  • Loading branch information
JoshuaKGoldberg and poteto authored Feb 11, 2025
1 parent a69b80d commit 192555b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/react/src/ReactHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ export function useEffect(
updateDeps?: Array<mixed> | 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 &&
Expand All @@ -118,6 +126,14 @@ export function useInsertionEffect(
create: () => (() => void) | void,
deps: Array<mixed> | 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);
}
Expand All @@ -126,6 +142,14 @@ export function useLayoutEffect(
create: () => (() => void) | void,
deps: Array<mixed> | 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);
}
Expand Down

0 comments on commit 192555b

Please # to comment.