Skip to content

fix: exclude React elements from deepmerge to prevent infinite recursion (fixes #4038) - #4080

Open
ErfanBagheri404 wants to merge 1 commit into
jaredpalmer:mainfrom
ErfanBagheri404:fix/4038-reactnode-deepmerge
Open

fix: exclude React elements from deepmerge to prevent infinite recursion (fixes #4038)#4080
ErfanBagheri404 wants to merge 1 commit into
jaredpalmer:mainfrom
ErfanBagheri404:fix/4038-reactnode-deepmerge

Conversation

@ErfanBagheri404

Copy link
Copy Markdown

Summary

Fixes #4038ReactNode validation errors crash Formik with Maximum call stack size exceeded from deepmerge.

Root cause

Validation error objects (fieldErrors, schemaErrors, validateErrors) are merged via deepmerge.all() in Formik.tsx. When an error value is a React element (e.g. a rich error message like <span>required</span>), deepmerge recurses into it — React elements are plain objects whose props can contain nested elements — and recurses indefinitely until the stack overflows.

Change

Pass a custom isMergeableObject to deepmerge that returns false for React elements (detected via $$typeof === Symbol.for('react.element')) and other non-plain objects (Date, RegExp, Promise). These values pass through untouched while normal plain-object error merging is unchanged.

Verification

Reproduced locally with a standalone script using formik's bundled deepmerge: nested React elements as validation errors overflow the stack without the option and merge cleanly with it. The fix matches the approach suggested in the issue.

  • node_modules/.bin/tsc could not run under this environment (no package-level tsconfig; tests use tsdx/babel), so the change was verified via isolated reproduction of the merge behavior rather than the full suite.

Validation error values may be arbitrary React nodes (rich error
messages rendered in the UI). React elements are plain objects whose
props can contain nested elements, so deepmerge recurses into them
indefinitely and throws 'Maximum call stack size exceeded'.

Add a custom isMergeableObject that marks React elements (and other
non-plain objects like Date, RegExp, Promise) as non-mergeable so they
pass through untouched.

Fixes jaredpalmer#4038
@vercel

vercel Bot commented Aug 19, 2026

Copy link
Copy Markdown

@ErfanBagheri404 is attempting to deploy a commit to the Formik Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 1b4cb90

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@codesandbox-ci

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to use ReactNode as validation error in React 19

1 participant