• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

352
Views
Formik form is being reset to initial values after API call

I have a MaterialUI Autocomplete field with some called from API. Near that field I have a button that calls API to refresh options (to load new, if some were created). But, somehow all other fields are being dropped to initial values. I know that because when I do this while editing existing instance and not creating a new one, fields are staying as they came from the server. Furthermore, I tested an API call without updating the state of options. Even after a simple await of axios call form values are being cleared.

Here is the autocomplete input:

<FastField
    name="club"
    options={clubs}
    optionsLoading={clubsLoading}
    shouldUpdate={(next, prev) =>
        shouldFastFieldRerender(next, prev, getIn)
    }
>
    {() => (
        <FormField.AutoComplete
            label="Клуб"
            name="club"
            value={values.club}
            setFieldValue={setFieldValue}
            setTouched={setFieldTouched}
            touched={touched.club}
            errors={errors.club}
            options={clubs}
            disabled={clubsLoading}
            selectionDisabled={clubsLoading}
        />
    )}
</FastField>

And here is the button near it:

<IconButton
    type="button"
    className={classNames(
        classes.btn,
        classes.reloadButton
    )}
    onClick={fetchClubs}
>
    <ReplayIcon />
</IconButton>

The fetch function:

const fetchClubs = React.useCallback(async () => {
    const clubs = await getClubs();

    setClubs(clubs);
}, [getClubs]);

So even if I comment the setClubs function and do only await getClubs(), the form will be reseted anyway

almost 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error