Repository navigation
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
Activity
LeCarbonator commentedon Oct 30, 2025
What happens if you use the explicitly reactive variants of all accessors?
The hook should cause a rerender on value change already, but perhaps this can help with figuring out the problem.
JamesHyot commentedon Oct 30, 2025
This does make the input reactive again, but so did this :
or this :
Because the docs do use
const field = useFieldContext<string>()and then access its properties in the rest of the components, I didn't think using field directly could cause any issueLeCarbonator commentedon Oct 30, 2025
The field itself is (mostly) stable. field.state is a getter of the current values, and it's set to rerender everytime values changes. However, that value rerender does not change the reference of field, so it seems like it was wrongly optimized away
crutchcorn commentedon Dec 1, 2025
@JamesHyot can you share a full reproduction with us in the form of a StackBlitz or Codesandbox? I think I have a fix.
elliason commentedon Dec 1, 2025
I am also having troubles with stale
field.state.value. What I do is getting the field context in one component:Then passing it to another component:
While the log in PreBoundDateField where field is returned from context shows the correct value, in DateField the value is stale and does not update.
@crutchcorn Would the PR above also fix this, or is this unrelated problem ? Thanks
crutchcorn commentedon Dec 1, 2025
I think it should!
Can you test your code against this PR?
#1893
And let me know? If not, please open a new issue and include a StackBlitz/Codesandbox
elliason commentedon Dec 1, 2025
I will try, thank you!
elliason commentedon Dec 1, 2025
🚀🔥 YES! Confirmed, It works great! Thank you very much!