-
-
Notifications
You must be signed in to change notification settings - Fork 541
Description
Describe the bug
When using useFieldContext with React Compiler and a ternary expression to render or not an input, the reactivity of the field breaks and its values is not updated when typing.
This is caused by the Compiler changing the dependency for a rerender from "field.state.value" to "field", which is always the same object so no UI update is made.
I have no idea if this is really an issue with @tanstack/form or the React Compiler itself, though I can get why the compiler would want to memo based on "field" and not only its subproperties.
This example doesn't work (and neither does a {canBeExtended && (<input value={field.state.value} onChange={(e) => field.handleChange(e.target.value)} />)} :
import { useFieldContext } from './form-context.tsx'
export function TextField({ label, canBeExtended }: { label: string, canBeExtended: boolean }) {
// The `Field` infers that it should have a `value` type of `string`
const field = useFieldContext<string>()
return (
<label>
<span>{label}</span>
{canBeExtended ?
(<input
value={field.state.value}
onChange={(e) => field.handleChange(e.target.value)}
/>)
: (<div className='extended'>
<input
value={field.state.value}
onChange={(e) => field.handleChange(e.target.value)}
/>
</div>)}
</label>
)
}
See the Compiler playground which outputs an if ($[2] !== canBeExtended || $[3] !== field) { which is different from the if ($[4] !== field.state.value || $[5] !== t2) { of this basic playground
There are several workarounds available to us, destructuring field, writing the ternary expression another way, or using a temp variable : "const val = field.state.value". However this issue caught me off-guard so I decided to submit it.
Your minimal, reproducible example
Steps to reproduce
Create a pre-bound field component which renders an input conditionnally
Add React Compiler
The field doesn't update :(
Expected behavior
The input should update when you type something in it
How often does this bug happen?
None
Screenshots or Videos
No response
Platform
Not OS or Browser specific
TanStack Form adapter
react-form
TanStack Form version
1.19.1
TypeScript version
5.9.2
Additional context
No response