I have options array for month and I am using it in useEffect
but getting warning for deps.
Kindly guide how to use useMemo
.
I am getting warning as given below:
The 'options' array makes the dependencies of useEffect
Hook (at line 83) change on every render. To fix this, wrap the initialization of 'options' in its own useMemo
Hook react-hooks/exhaustive-deps
const options = [];
useEffect(() => {
if (financialMonth) {
options.forEach((item) => {
if (Number(item.value) === Number(financialMonth)) {
setMonthDefault(item.text);
}
});
if (financialMonth.length === 0) setMonthDefault("April");
}
}, [financialMonth, options]);
CodePudding user response:
Simply:
const options = useMemo(() => [], [])
If you have a constant array of values (if it does not depend on state or props), then consider putting it outside of the component. That way you won't have to worry about useMemo
or having it in the useEffect
dependency array.