Initialize form with async values #13254
Replies: 2 comments 1 reply
-
|
Would the
// set default value sync
function App({ values }) {
useForm({
values, // will get updated when values props updates
})
}
function App() {
const values = useGetAsyncData();
useForm({
defaultValues,
values, // will get updated once values returns
})
}Otherwise, having separate components seems like the easiest option. This shouldn't require too much extra plumbing. function FormContainer() {
const data = useGetAsyncData();
if (!data) return null;
return <Form data={data} />;
}
function Form({ data }) {
const form = useForm({
defaultValues: data,
});
} |
Beta Was this translation helpful? Give feedback.
-
|
The problem when I try to use The log I get is: So the values lag by one render. This still makes it a mess to initialize the form in a clean way. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Im currently in a project with certain limitations that makes it very hard for me to use react-hook-form. This setup is very common:
But obviously it is much more complex in the real world. Many data hooks, huge forms. The problem is that defaultValues are first initialized without the data, or part of the data, while the data is loaded.
The data must be received through this hook, so I cant use react-hook-form async defaultValues.
I could add a
useEffectthat resets the form, but this would still result in a render where the form is initialized with the wrong values, which causes problems.I could break out data and form into two different components, and load the form-component only when all data has loaded. But often there are many data-hooks and it would result in very much data plumbing.
So there are many solution, but they all feel quite ugly. I wish
useFormcould take a value likewaitForDatathat just told it to wait with initializing the form.Am I missing something? Would a
waitForDataprop be a good solution?Beta Was this translation helpful? Give feedback.
All reactions