Skip to content

[Fiber] Warn when useMemo is called with a non-function first argument#36301

Open
ousamabenyounes wants to merge 1 commit intofacebook:mainfrom
ousamabenyounes:fix/issue-16589
Open

[Fiber] Warn when useMemo is called with a non-function first argument#36301
ousamabenyounes wants to merge 1 commit intofacebook:mainfrom
ousamabenyounes:fix/issue-16589

Conversation

@ousamabenyounes
Copy link
Copy Markdown

Summary

Fixes #16589.

When useMemo was called with a non-function first argument (a common mistake, e.g. passing the memoized value directly instead of a factory), React threw TypeError: nextCreate is not a function from inside mountMemo. The stack pointed at React internals and most developers had no idea what nextCreate referred to — the error didn't name the hook or the component involved.

Add a DEV-only warning mirroring the existing Expected useImperativeHandle() second argument to be a function warning. The DEV warning fires before the TypeError and names the hook plus the actual typeof of the value received, so the developer sees a component-attributed message pointing at their own code:

Expected useMemo() first argument to be a function that returns a value. Instead received: object.
in App (at …)

Prod behaviour is unchanged — the TypeError: nextCreate is not a function still surfaces as before.

Repro

function App() {
  // Common mistake — `useMemo` expects a factory, not the value itself.
  const value = useMemo({result: 1}, []);
  return value.result;
}

Before: TypeError: nextCreate is not a function with a stack inside react-dom.development.js, no hint at which component/hook.
After: DEV warning names useMemo() + the actual type (object) with a component stack, then the TypeError surfaces if the bug isn't fixed.

How did you test this change?

  • New test warns when useMemo is called with a non-function first argument in ReactHooks-test.internal.js — models on the neighbouring warns for bad useImperativeHandle … tests.
  • yarn test ReactHooks-test: 73 passed / 0 failed (72 baseline + 1 new).
  • yarn test --prod ReactHooks-test: 72 passed / 0 failed — the assertConsoleErrorDev assertion is a no-op in prod and the rejects.toThrow still holds.
  • yarn test ReactHooksWithNoopRenderer-test: 96 passed / 0 failed.
  • yarn test useMemoCache: 7 passed / 0 failed.
  • yarn flow dom-node — clean.
  • yarn linc — clean.
  • yarn prettier — clean.

🤖 Generated with Claude Code

When `useMemo` received something other than a function (an object, an
array, `null`, etc.) React would throw `TypeError: nextCreate is not a
function` from inside `mountMemo`. The error pointed at React internals
and left developers wondering what `nextCreate` was and why their
object wasn't accepted (fixes facebook#16589).

Add a DEV-only warning — mirroring the existing
`Expected useImperativeHandle() second argument to be a function`
warning — that names the hook and the actual type received, so the
developer sees a clear message pointing back at their component before
the TypeError surfaces. Prod behaviour is unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
@meta-cla meta-cla bot added the CLA Signed label Apr 17, 2026
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.

Confusing error when passing non-function to useMemo

1 participant