Skip to content

Improvement: useEffect/useInsertionEffect/useLayoutEffect null-check warning messages are vague and don't distinguish null vs undefined #36362

@BalajiRKB

Description

@BalajiRKB

Summary

The dev-mode console.warn messages for useEffect, useInsertionEffect, and useLayoutEffect in packages/react/src/ReactHooks.js are currently vague when the create callback is null or undefined.

Current Behavior

All three hooks produce this generic message:

React Hook useEffect requires an effect callback. Did you forget to pass a callback to the hook?

This message:

  • Does not distinguish between null and undefined being passed
  • Does not explain what the callback signature should look like
  • Leaves beginners confused about what the correct fix is

Expected Behavior

The warning should:

  1. Say explicitly whether null or undefined was received
  2. Explain that the hook expects a function that optionally returns a cleanup function
  3. Be consistent across all three effect hooks

Example improved message:

React Hook useEffect received a null effect callback. useEffect must be passed a function that optionally returns a cleanup function. Did you accidentally pass nothing or the result of a function call instead of the function itself?

Motivation

This is a very common beginner mistake — for example, passing useEffect(someAsyncFn(), []) (calling the function) instead of useEffect(someAsyncFn, []) (passing the function). A clearer error message would surface the root cause faster.

Fix

A fix has been submitted in PR #36361.

Affected Files

  • packages/react/src/ReactHooks.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions