[Fiber] Warn when useMemo is called with a non-function first argument#36301
Open
ousamabenyounes wants to merge 1 commit intofacebook:mainfrom
Open
[Fiber] Warn when useMemo is called with a non-function first argument#36301ousamabenyounes wants to merge 1 commit intofacebook:mainfrom
ousamabenyounes wants to merge 1 commit intofacebook:mainfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #16589.
When
useMemowas called with a non-function first argument (a common mistake, e.g. passing the memoized value directly instead of a factory), React threwTypeError: nextCreate is not a functionfrom insidemountMemo. The stack pointed at React internals and most developers had no idea whatnextCreatereferred 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 functionwarning. The DEV warning fires before the TypeError and names the hook plus the actualtypeofof the value received, so the developer sees a component-attributed message pointing at their own code:Prod behaviour is unchanged — the
TypeError: nextCreate is not a functionstill surfaces as before.Repro
Before:
TypeError: nextCreate is not a functionwith a stack insidereact-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?
warns when useMemo is called with a non-function first argumentinReactHooks-test.internal.js— models on the neighbouringwarns 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 — theassertConsoleErrorDevassertion is a no-op in prod and therejects.toThrowstill 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