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