-
-
Notifications
You must be signed in to change notification settings - Fork 428
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
Comments
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. |
I just went back and you're absolutely right it doesn't 'crash' not sure why I described it that way, my apologies. |
Pretty sure this is caused by us calling setFieldValue directly on the constructor of FieldApi here: form/packages/form-core/src/FieldApi.ts Lines 999 to 1002 in 72b4572
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! |
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
}
/> |
@asegarra has this been resolved for you in latest? |
Yes it's fixed, thank you! |
Describe the bug
React crashes if you set a Field level defaultValue on a conditionally rendered Field.
Your minimal, reproducible example
https://codesandbox.io/p/sandbox/optimistic-golick-tkj4dz
Steps to reproduce
Expected behavior
Should not crash
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
TanStack Form adapter
None
TanStack Form version
v0.41.3
TypeScript version
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: