TanStackT
TanStack6mo ago
8 replies
uncertain-scarlet

Async form default based on another field

Is there a clean way to base an async form field default off of the value of another field? The issue with using the form's default values option is that it creates a circular dependency.

Specific example, I have items which have an "inventory type" option. Inventory type can be retail or consignment let's say. Each item is connected to an account which has its own default for inventory type.

So if the selected account is set to one which defaults to "consignment", I need to update the form default after fetching the account.

The issue is that the fetch must be declared after the useForm hook because the fetch is using values from the form. This is an issue because then that default value is defined after the form.

A workaround could be to store the defaults in a useState, and then set that state from the fetch, but that's a bit messy.

Another workaround could be to set the default on the field, but that doesn't work because of this issue which has been closed as intended behavior.

A final workaround (which is suggested in the issue conversation by me) would be to manually set the field value in a useEffect if the field is not dirty. This doesn't work always if the form is reset, and there doesn't appear to be any way to tell in a useEffect if a form is reset.

Any suggestions are appreciated!
GitHub
Describe the bug When using the defaultValue prop on form field, if the value is updated, the default doesn't update when the prop is updated. I could understand maybe if this was intentional b...
Default value prop on field doesn't update · Issue #1154 · TanSta...
Was this page helpful?