How to properly do async initial values? #1812
-
I've tried following https://tanstack.com/form/latest/docs/framework/react/guides/async-initial-values, but I have some problems with this approach. If I do const form = useAppForm({
defaultValues: dataFromServer, // can be undefined
}) there is one render that happens when I've been able to work around this with: const isMissingId = useStore(form.store, (s) => !s.values.id)
if (isMissingId) return null; but it feels tedious and error-prone to do it this way. I'm hoping I'm missing something? It seems very weird to me that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
What is the type of The best practice for async initial values is to have an 'empty' version of your data while the request is pending. All strings are empty strings, all Dates are today, all numbers are zero etc. It doesn't need to be recreated on every render since it's just a static fallback object, so you can place it outside of the component. const form = useAppForm({
defaultValues: dataFromServer ?? emptyValues
}) |
Beta Was this translation helpful? Give feedback.
What is the type of
dataFromServer
? It should warn you that it is possibly undefined, which should cascade to the fields.The best practice for async initial values is to have an 'empty' version of your data while the request is pending. All strings are empty strings, all Dates are today, all numbers are zero etc.
It doesn't need to be recreated on every render since it's just a static fallback object, so you can place it outside of the component.