`form.Subscribe` API inconsistency: children prop vs JSX children #1976
Send to my agentDescribe the bug#
form.Subscribe doesn't accept children as a prop attribute, only as JSX children, but the examples given showing that it accept children as props.
When using form.Subscribe with a selector prop, passing the render function via the children prop attribute doesn't work. The component only works when the render function is passed as JSX children (between opening and closing tags).
This creates an inconsistency with other form components like form.AppField, which accept children as a prop attribute.
Eventually both pattern works as expected but typescript complaining about the error.
Your minimal, reproducible example#
https://stackblitz.com/edit/vitejs-vite-of4hmkxe?file=src%2FApp.tsx
Steps to reproduce#
- Create a form using
withFormoruseForm - Use
form.Subscribewith aselectorprop - Pass the render function via the
childrenprop attribute
<form.Subscribe
children={(title) => {
return <div>{title}</div>
}}
selector={(state) => state.values.title}
/>
Expected behavior#
The form.Subscribe component should accept the render function via the children prop attribute, similar to how form.AppField works:
// This pattern should work (but currently doesn't)
<form.Subscribe
children={(title) => <div>{title}</div>}
selector={(state) => state.values.title}
/>
// This pattern works for form.AppField
<form.AppField
children={(field) => <input {...field} />}
name="title"
/>
How often does this bug happen?#
Every time
Screenshots or Videos#
Selector error:
Props error:
Platform#
- OS: Mac OS
- Version: Sequoia 15.5
TanStack Form adapter#
react-form
TanStack Form version#
^1.27.7
TypeScript version#
^5.7.2
Additional context#
No response
LeCarbonator(GitHub profile, opens in a new tab) added labelsquestion
Here's a TypeScript playground to demonstrate the inference.
There's not much we can do about this since it is related to React and not TanStack Form. If you have ideas on how to approach this differently, let us know.
LeCarbonator(GitHub profile, opens in a new tab) closed this
Ah I see!, Thanks for the clarification. I wasn't aware that TypeScript infers generic types sequentially through props and I just learned today. This make sense now, the prop order determines the inference order, while JSX children always processed after all props.
In my case, the props were being automatically reordered by Biome. which was moving
childrenbeforeselectorand causing TypeScript error.Thank you very much @LeCarbonator !
I had a suspicion that the property sorting was the cause. We've had reports for Biome environments before, and it's one of the few reasons that property sorting can be iffy in TypeScript.
Keep in mind that TanStack Router can also change behaviour if you change the order of properties to be alphabetical.
Sign in to join this conversation.
This happens because of how React props work.
To put it simply, the props look sort of like this:
The order here is important. If
childrenwas placed first, it would be inferred asunknownand reject your selector.If the selector comes first, then the children properly accepts that value.
This is fine with JSX children, since
childrenwill always be second:The example you posted is the "wrong" order though, and will cause an error. You can still make it work, as long as
childrenis placed afterselector:The important thing to note:
childrenas prop vs. as JSX child is a semantic difference only. Both are valid in React and not in our control.