Skip to content

Commit 192555b

Browse files
Added dev-only warning for null/undefined create in use*Effect (#32355)
## 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 <[email protected]>
1 parent a69b80d commit 192555b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

packages/react/src/ReactHooks.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ export function useEffect(
9393
updateDeps?: Array<mixed> | void | null,
9494
destroy?: ((resource: {...} | void | null) => void) | void,
9595
): void {
96+
if (__DEV__) {
97+
if (create == null) {
98+
console.warn(
99+
'React Hook useEffect requires an effect callback. Did you forget to pass a callback to the hook?',
100+
);
101+
}
102+
}
103+
96104
const dispatcher = resolveDispatcher();
97105
if (
98106
enableUseEffectCRUDOverload &&
@@ -118,6 +126,14 @@ export function useInsertionEffect(
118126
create: () => (() => void) | void,
119127
deps: Array<mixed> | void | null,
120128
): void {
129+
if (__DEV__) {
130+
if (create == null) {
131+
console.warn(
132+
'React Hook useInsertionEffect requires an effect callback. Did you forget to pass a callback to the hook?',
133+
);
134+
}
135+
}
136+
121137
const dispatcher = resolveDispatcher();
122138
return dispatcher.useInsertionEffect(create, deps);
123139
}
@@ -126,6 +142,14 @@ export function useLayoutEffect(
126142
create: () => (() => void) | void,
127143
deps: Array<mixed> | void | null,
128144
): void {
145+
if (__DEV__) {
146+
if (create == null) {
147+
console.warn(
148+
'React Hook useLayoutEffect requires an effect callback. Did you forget to pass a callback to the hook?',
149+
);
150+
}
151+
}
152+
129153
const dispatcher = resolveDispatcher();
130154
return dispatcher.useLayoutEffect(create, deps);
131155
}

0 commit comments

Comments
 (0)