I’m working on a React application using react-hook-form and @mui/material, and I have a form that includes an Autocomplete component for selecting multiple values. The issue I’m facing is that when I submit the form, the value of the Autocomplete field resets to its initial state.
onSubmit={handleSubmit((data) => {
onSearch(createSearchData(data));
})}
<Controller
control={control}
name="industries"
render={(renderProps) => (
<Autocomplete
multiple
filterSelectedOptions
options={industriesValues}
onChange={(_, newValue) =>
renderProps.field.onChange(newValue)
}
value={renderProps.field.value}
renderInput={(params) => (
<TextField
{...params}
label="Industries"
helperText={renderProps.fieldState.error?.message}
error={Boolean(renderProps.fieldState.error?.message)}
/>
)}
/>
)}
/>
I’ve tried to use reset with value stored in another variable inside onSubmit but it doesn’t look like it has any effect. In the same form I have the FormTextFields which don’t reset after triggering onSubmit so I guess the issue is somewhere in Controller.
kubapro84 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.