RFC: async defaultValues #9046
Replies: 68 comments 14 replies
|
With |
|
I think this is a great idea. During my day job we work with large data sets, and we have specific queries to get available data for dropdown fields. If this was implemented it would pave the way to have a loading indicator and then some dropdown field all built without a Controller. The Async Promise seems like the best way forward (to me). |
|
Thanks @JeromeDeLeon I think you can pass arguments into that Promise callback. useForm({
defaultValues: async (args) => {
if (args.test) { return { data: 'test' }; }
}
})happy for alternative solutions as well. I think i see what do you mean now... yes defaultValue will be executed once at the first invoke. |
|
Got it so its's not just about the API or anything. That makes sense at least to that part 😃 |
|
Do we need to provide another state like |
Good point probably will leave the user to define that state.
|
|
You mean let the user define |
useForm({
defaultValues: async (args) => {
if (args.test) { return { data: 'test' }; }
}
})
|
|
unless we wrap it with |
Is it possible to do the following? useForm({
defaultValues: async () => {
const result = await fetch("xxxxx").then(res => res.json());
return result;
}
}) |
|
That's my question earlier, because what if you rely on something like |
|
You can still display your form, and wait for the promise to resolved and inject to all the inputs. if you need to show a loading spinner. you can just do a |
useForm({
defaultValues: async () => {
updateLoading(false)
const result = await fetch("xxxxx").then(res => res.json());
updateLoading(true)
return result;
}
})that works? |
|
I guess |
|
Does this case work ? |
|
@zeddz92 |
|
What's the best way to achieve this in 2022 ? First I thought the best way is to not mount (and call useForm) as long data are not fetched. |
|
I'm facing the same issue with here is my code At first |
|
here is my solution with useForm: |
|
Any updates on this? I'm not a big fan of the My current solution is to add a mid-level wrapper (to make sure async data is present). And call the |
|
React 18 releases a new function "Suspense". Sorry, no concrete idea. |
|
Hello ! Did you consider handling error state during async fetch ? If you have an idea on how can we handle it, I would be pleased 🙏 |
|
How to handle errors during async function? When I do async and try to catch the error then type of defaultValues is screaming that |
|
If anyone came here wondering what would be the best option to handle async default values that came from a react-query hook for example, the recommendation is to use the |
|
There are two options suggested here:
There is no solution to set the default values for values that come from another hook (like |
|
is there a native way to handle errors due when the async defaultValue errored out? i.e, network timeout, server returned 400+ code? i use |
|
@bluebill1049 Should we update the first post here to include this was released? #9261 |
|
Has the AFAIK it doesn't seem to be working; I've made a small demo here on how I think it might work, let me know if it's wrong or could be improved |


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Context
The current status of setting
defaultValuesfrom async data source is usingreset:honestly, this is probably not a pretty and clean approach... a few weeks before we had a feature request regarding async
defaultValues: #2335Proposal Solutions
Option A
defaultValuesacceptPromise, and the promise will get resolved, although I do have concern over the confusion about which prop supports bothobjectandPromise.What's happening here:
resetAPI will get invoked to reset the form and hence provide reset optionsOption B
valuesfor form values updatehappy to discuss more options on the table too.
Update 27/10/2022: going to update and support both
valuesand asyncdefaultValuesPOC
PR: #9052
cc @leapful @Moshyfawn
All reactions