RFC: async defaultValues #9046
Replies: 65 comments 6 replies
-
With |
Beta Was this translation helpful? Give feedback.
-
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). |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
Got it so its's not just about the API or anything. That makes sense at least to that part 😃 |
Beta Was this translation helpful? Give feedback.
-
Do we need to provide another state like |
Beta Was this translation helpful? Give feedback.
-
Good point probably will leave the user to define that state.
|
Beta Was this translation helpful? Give feedback.
-
You mean let the user define |
Beta Was this translation helpful? Give feedback.
-
useForm({
defaultValues: async (args) => {
if (args.test) { return { data: 'test' }; }
}
})
|
Beta Was this translation helpful? Give feedback.
-
unless we wrap it with |
Beta Was this translation helpful? Give feedback.
-
Is it possible to do the following? useForm({
defaultValues: async () => {
const result = await fetch("xxxxx").then(res => res.json());
return result;
}
}) |
Beta Was this translation helpful? Give feedback.
-
That's my question earlier, because what if you rely on something like |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
useForm({
defaultValues: async () => {
updateLoading(false)
const result = await fetch("xxxxx").then(res => res.json());
updateLoading(true)
return result;
}
}) that works? |
Beta Was this translation helpful? Give feedback.
-
I guess |
Beta Was this translation helpful? Give feedback.
-
Does this case work ?
|
Beta Was this translation helpful? Give feedback.
-
The proposed solution to this has a gotcha: if you update the default value object, you are resetting the entire form to the default values. Obviously, you don't want to do this while a user is potentially entering data. Thus, any async default values should be resolved prior to initial render of the form, with a
This kind of sucks since, intuitively, changing the default values of a form should not reset the entire form state. However, after reading through the RFC comments, I honestly can't think of a better solution either. |
Beta Was this translation helpful? Give feedback.
-
If you already set the defaultValue for each input like this
You can just call
|
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
@zeddz92 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
I'm facing the same issue with here is my code
At first |
Beta Was this translation helpful? Give feedback.
-
here is my solution with useForm:
|
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
React 18 releases a new function "Suspense". Sorry, no concrete idea. |
Beta Was this translation helpful? Give feedback.
-
Beta: #9381 |
Beta Was this translation helpful? Give feedback.
-
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 🙏 |
Beta Was this translation helpful? Give feedback.
-
How to handle errors during async function? When I do async and try to catch the error then type of defaultValues is screaming that |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
There are two options suggested here:
There is no solution to set the default values for values that come from another hook (like |
Beta Was this translation helpful? Give feedback.
-
Context
The current status of setting
defaultValues
from 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
defaultValues
acceptPromise
, and the promise will get resolved, although I do have concern over the confusion about which prop supports bothobject
andPromise
.What's happening here:
reset
API will get invoked to reset the form and hence provide reset optionsOption B
values
for form values updatehappy to discuss more options on the table too.
Update 27/10/2022: going to update and support both
values
and asyncdefaultValues
POC
PR: #9052
cc @leapful @Moshyfawn
Beta Was this translation helpful? Give feedback.
All reactions