Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions packages/react/src/ReactHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(resourceType: () => T): T {
const dispatcher = ReactSharedInternals.A;
if (!dispatcher) {
Expand Down Expand Up @@ -88,14 +115,7 @@ export function useEffect(
create: () => (() => void) | void,
deps: Array<mixed> | 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);
}
Expand All @@ -104,14 +124,7 @@ 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?',
);
}
}

warnOnMissingEffectCallback('useInsertionEffect', create);
const dispatcher = resolveDispatcher();
return dispatcher.useInsertionEffect(create, deps);
}
Expand All @@ -120,14 +133,7 @@ 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?',
);
}
}

warnOnMissingEffectCallback('useLayoutEffect', create);
const dispatcher = resolveDispatcher();
return dispatcher.useLayoutEffect(create, deps);
}
Expand Down