How to load Form asynchronously?

1 Answer 96 Views
Form
Yiheng
Top achievements
Rank 2
Iron
Iron
Iron
Yiheng asked on 28 Jun 2022, 09:21 PM

Hi there, I'm trying to use a state hook and an effect hook to update the initialValues, but the form fields are not filled after the initialValues is updated. Is that an expected behavior? If so, how could I load the Form after I fetched initialValues from api? Thank you.

"@progress/kendo-react-form": "^5.4.0",

Yiheng
Top achievements
Rank 2
Iron
Iron
Iron
commented on 29 Jun 2022, 11:00 AM

https://stackblitz.com/edit/react-ns48nl?file=app%2Fmain.jsx

1 Answer, 1 is accepted

Sort by
0
Accepted
Yiheng
Top achievements
Rank 2
Iron
Iron
Iron
answered on 29 Jun 2022, 11:17 AM | edited on 29 Jun 2022, 11:18 AM

It turns out that I also need to update the key, https://www.telerik.com/kendo-react-ui/components/form/advanced-scenarios/#toc-resetting-the-form-initial-state

      <Form
        onSubmit={handleSubmit}
        initialValues={init}
        key={JSON.stringify(init)}
Konstantin Dikov
Telerik team
commented on 29 Jun 2022, 12:26 PM

Hi Yiheng,

As you have found out, in order to update the initialValue for the Form you will also have to update the "key" value, so that the Form can re-mount with the new values.

In order to avoid any issues with the key values, you can use "new Date()" or some incrementing value instead, which will ensure that you are always setting unique value.

Please let us know if further assistance is needed.

Yiheng
Top achievements
Rank 2
Iron
Iron
Iron
commented on 29 Jun 2022, 12:28 PM

Thanks for your suggestion.
Tags
Form
Asked by
Yiheng
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Yiheng
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or