Updating defaultValue and value at different paths in the form?
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 6.7k
- Forks
- 682
- Avg merge
- 5d 18h
- Merged PRs (30d)
- 7
Description
Describe the bug
Note: I created this as a bug because I tried some api(s) to achieve what's mentioned below (like
field.update()) but couldn't make it work and ran into errors. Please feel free to move it to discussions section if you think that's appropriate.
We are considering using TanStack form for our CMS content form as it really simplifies a lot of stuff for us, but we have an interesting challenge with TanStack Form that we previously faced with React Hook Form, hoping there might be a better solution. The issue comes up in our dynamic form where we have a list of items with nested data that loads on demand.
Here’s a simple example:
const form = useForm({
defaultValues: {
fruits: [
{
id: 'A',
name: 'apple',
attributes: null // loaded when expanded
},
{
id: 'B',
name: 'banana',
attributes: null
}
]
}
});
When a user expands a fruit item, we load its attributes. This works fine normally, but gets tricky when items are reordered before expanding. For example, if a user moves banana to the first position:
// Current form values after reordering:
fruits: [
{ id: 'B', name: 'banana', attributes: null }, // moved here
{ id: 'A', name: 'apple', attributes: null }
]
// But defaultValues are still:
defaultValues.fruits: [
{ id: 'A', ... },
{ id: 'B', ... } // banana's defaults still here at index 1
]
Now when banana is expanded and we load its attributes, we need to:
- Set the defaultValue at fruits[1].attributes (where banana’s defaults live)
- Set the current value at fruits[0].attributes (where banana currently is)
In React Hook Form, we had to use resetField() which always updates both defaultValue and value at the same path. This forced us to use some hacks workarounds to maintain the correct state.
Is there a cleaner way in TanStack Form to:
- Update a field’s defaultValue at one path without affecting its current value? Or
- Update defaultValue and current value at different paths?
Your minimal, reproducible example
N/A
Steps to reproduce
We have a form with nested fields that load their data on demand. For example, when a parent item expands, we fetch and load its nested field data. This becomes tricky when the parent items have been reordered. When an item moves positions, its current value moves to the new position but its default value stays at the original position in the form state. So when we load the nested field data after expanding a moved item, we need to update its defaultValue at the original position but its current value at the new position. However, we can’t find a way in TanStack Form to update defaultValue and current value at different paths.
Expected behavior
Ability to update a field’s defaultValue and current value at different paths in the form state, or some way to update just the defaultValue of a field without affecting its current value.
How often does this bug happen?
None
Screenshots or Videos
No response
Platform
MacOS
TanStack Form adapter
react-form
TanStack Form version
1.3.0
TypeScript version
5.7.2
Additional context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the form state APIs mentioned in the issue, especially field.update and resetField, and trace how defaultValues and current values are stored for reordered nested fields. Done means establishing whether the API can update a default value independently or apply default and current values at different paths, with the behavior verified for the described reordered fruits case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- frontend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100