form icon indicating copy to clipboard operation
form copied to clipboard

React warning when setting defaultValue on conditionally rendered Field.

Open asegarra opened this issue 9 months ago • 4 comments

Describe the bug

React crashes if you set a Field level defaultValue on a conditionally rendered Field.

 Cannot update a component (`App`) while rendering a different component (`Field`). To locate the bad setState() call inside `Field`, follow the stack trace as described in https://react.dev/link/setstate-in-render 
    at Field (https://tkj4dz.csb.app/node_modules/@tanstack/react-form/dist/esm/useField.js:43:23)
    at APIField
    at div
    at form
    at div
    at App

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/optimistic-golick-tkj4dz

Steps to reproduce

  1. Type something in First name so last name gets rendered

Expected behavior

Should not crash

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Windows 10

TanStack Form adapter

None

TanStack Form version

v0.41.3

TypeScript version

No response

Additional context

No response

asegarra avatar Jan 30 '25 17:01 asegarra