New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recommended way to set initialValues from async api call #1033
Comments
Are you using this component inside some other component from where you are passing data? A bit more code block sample will make the community easy to understand. |
You pass them as props to initialValues |
I think he is already using props but using Redux's HOC(connect). |
@dem0nbruce Just have a default value for const { user = {} } = this.props;
return (
<Formik enableReinitialize={true} initialValues={user}>
...
</Formik>
) |
@davemooreuws Your solution defaulting the user field values worked for me. |
What if I make update data in form? It's necessary to save last loaded input values in form as default values. In this case initial data will change, and cannot be empty. |
@davegahn yeah, I have similar case. Would like to have an ability to merge updated form with new initial values myself @jaredpalmer is there a way to do that right now? |
@aleksey-pro did you ever resolve that use case? I am running into the same issue at the moment - unable to keep field values from other fields when the API-dependent field is updated via |
I believe you have the same issue i had. I am fetching the initialized formik data onComponentMount, and was getting the stated error above. The following gets called when loading the component.
On the Formik Component i set the following:
The key is getData: Where tempData is just an object with the field value keys: |
I set the Formik's initial value to a local state and updated that state when I got new data to populate the form. It worked after I set enableReinitialize to True |
I am using useFormik instead , how do I set the values from api call in my redux action passed as props to my component |
I really don't understand how it works... |
|
but how can you have default values for fields you don't know? |
I think in that case you can pass the value prop to once you receive the form fields and their default values from your API call asynchronously |
@kilinkis I don't have an answer at the moment because your request isn't supported in the Formik API. But if you are in control of your API and able to create a new build when updating the fields in your form (CMS ??) you can trigger a github action to re-run your build, make your API call to Fields during your build which populates the default initial values, and use those to prepopulate your form. Or if your page is being printed by a server with access to that info, WordPress as an example, None of these are ideal, for sure, and in the future maybe this information could be obtained via registerField. |
We can use shorted code for default value const { user } = this.props;
|
You can subscribe in props and when props will be change, you can create need fields and initialValues for them. Into initialValue you put method which well be return new genereted default values. So as |
My use case was to handle both 'Create' and 'Update' actions in the same page/form. Awesome stuff 👍 🥃 |
can you share the example of get api call for enablereinitialize value need to bind using functional components? |
If you are getting your initial values from an API call then they are technically NOT initial values. They are the first state after initial values. So you should set your initial state to something like empty values, and then pass down the updated values from the API to the specific formik components and use something like setFieldValue or value prop. |
some of my form fields weren't updating after async call. My issue was that i set initial field value to undefined instead of an empty string. Changing it back to empty string solved the problem. |
@iblokhin You saved my problem. Thank you. |
❓Question
What is the recommended/best practice way to set the forms values when loading data from an asynchronous api call?
My use case involves using redux to load data into my components props and binding it to
initialValues
:This however throws the following error because
this.props.user
is initially undefined:Is this the recommended/best practice way to load data into a form? If so, how would I resolve the error above?
The text was updated successfully, but these errors were encountered: