Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React warning when setting defaultValue on conditionally rendered Field. #1134

Closed
asegarra opened this issue Jan 30, 2025 · 6 comments
Closed

Comments

@asegarra
Copy link

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

@crutchcorn crutchcorn changed the title React crash when setting defaultValue on conditionally rendered Field. React warning when setting defaultValue on conditionally rendered Field. Feb 21, 2025
@crutchcorn
Copy link
Member

This doesn't seem to crash for me, but agreed we should look into fixing at some point. Help from the community would be appreciated here.

@asegarra
Copy link
Author

I just went back and you're absolutely right it doesn't 'crash' not sure why I described it that way, my apologies.

@juanvilladev
Copy link
Contributor

juanvilladev commented Mar 3, 2025

Pretty sure this is caused by us calling setFieldValue directly on the constructor of FieldApi here:

if (opts.defaultValue !== undefined) {
this.form.setFieldValue(this.name, opts.defaultValue as never, {
dontUpdateMeta: true,
})

When you defined a defaultValue, and the component is rendering, within the same cycle we're attempting to update state which seems to violate a React rule about this. You'll notice in your stackblitz if you comment out defaultValue on lastName issue goes away.

Typically you'd end up having a useEffect that runs once the component is mounted, but this won't work for us since this is framework agnostic. I'll take a crack at this after #1201!

@juanvilladev
Copy link
Contributor

juanvilladev commented Mar 3, 2025

Update: a simple workaround right now is to not conditionally render the whole form.Field, but rather just content within the children. This way we ensure the Field is initiated despite the DOM not showing anything.

Example:

 <form.Field
            name="lastName"
            defaultValue="Smith"
            validators={{
              onChange: ({ value }) =>
                !value
                  ? "A last name name is required"
                  : value.length < 3
                  ? "last name must be at least 3 characters"
                  : undefined,
            }}
            children={
              firstName // if firstName not falsy render underlying component else null
                ? (field) => (
                    <>
                      <label htmlFor={field.name}>Last Name:</label>
                      <input
                        id={field.name}
                        name={field.name}
                        value={field.state.value}
                        onBlur={field.handleBlur}
                        onChange={(e) => field.handleChange(e.target.value)}
                      />
                      <FieldInfo field={field} />
                    </>
                  )
                : null
            }
          />

@juanvilladev
Copy link
Contributor

@asegarra has this been resolved for you in latest?

@asegarra
Copy link
Author

@asegarra has this been resolved for you in latest?

Yes it's fixed, thank you!

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

No branches or pull requests

3 participants