The values from a dropdown is based on an API call whose array response is stored into a Zustand store. I have a useEffect that listens to when that Zustand store variable is no longer empty, it will automatically assign the first value in that array as the selected value in the form.
const formik = useFormik()
const store = useZustandStore()
useEffect(() => {
if (store.someSearchItems.length) {
const initial = store.someSearchItems[0].value as string;
formik.setFieldValue('LIST_VALUE', initial);
}
}, [store.someSearchItems]);
It does its job, however, I get the error React Hook useEffect has a missing dependency: 'formik'. Either include it or remove the dependency array.
How do I workaround this lint issue without disabling it?
I tried including formik
in the dependency array, but that executes the useEffect even if unrelated values inside the form is changed.
2
Answers
You can do this:
Alternatively, you can suppress the warning with this comment:
// eslint-disable-next-line react-hooks/exhaustive-deps
Although, you can read more on why it is dangerous here
The result of
useFormik
(yourformik
constant) changes on each render, because it contains the form values. However,setFieldValue
(and other methods) are stable.Instead of using the entire
formik
object, destructuresetFieldValue
, and use it in theuseEffect
, and it’s dependencies.