Skip to content

fix: improve null/undefined warning messages for useEffect, useInsertionEffect, and useLayoutEffect#36361

Open
BalajiRKB wants to merge 1 commit intofacebook:mainfrom
BalajiRKB:fix/useEffect-null-check-warning-message
Open

fix: improve null/undefined warning messages for useEffect, useInsertionEffect, and useLayoutEffect#36361
BalajiRKB wants to merge 1 commit intofacebook:mainfrom
BalajiRKB:fix/useEffect-null-check-warning-message

Conversation

@BalajiRKB
Copy link
Copy Markdown

Summary

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

Before (all three hooks had this):

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

This message doesn't distinguish between null and undefined, and doesn't explain what the callback should look like, which is a common source of confusion for beginners.

After:

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?

Changes

  • useEffect — improved warning to mention null or undefined explicitly and explain the expected signature
  • useInsertionEffect — same improvement, consistent phrasing
  • useLayoutEffect — same improvement, consistent phrasing

All three hooks now use template literals to dynamically include whether the value was null or undefined, making it easier for developers to quickly understand and fix the issue.

Test Plan

No new tests required — this is a dev-mode warning message improvement only. Existing tests for null callback behaviour remain unchanged.

Related

Addresses common confusion reported by beginners around vague hook error messages.

…ffect, and useLayoutEffect

The existing dev-mode warning for null/undefined `create` callback in
useEffect, useInsertionEffect, and useLayoutEffect only mentions a
missing callback but does not distinguish between `null` and `undefined`,
which is a common source of confusion for beginners.

This commit improves the warning messages to:
- Mention `null` or `undefined` explicitly
- Suggest the most common fix (returning a cleanup function or nothing)
- Keep messages consistent across all three effect hooks
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 28, 2026

Hi @BalajiRKB!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@BalajiRKB
Copy link
Copy Markdown
Author

This PR fixes the issue reported in #36362.

The vague warning messages for useEffect, useInsertionEffect, and useLayoutEffect when a null or undefined callback is passed have been improved to:

  • Explicitly mention whether null or undefined was received
  • Explain the expected function signature
  • Stay consistent across all three hooks

@meta-cla meta-cla Bot added the CLA Signed label Apr 28, 2026
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 28, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

'React Hook useEffect requires an effect callback. Did you forget to pass a callback to the hook?',
`React Hook useEffect received a ${create === null ? 'null' : 'undefined'} 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?',
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Did you accidentally pass nothing" phrasing is confusing when create === null, because the user did pass something — they passed null explicitly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants