I want to change the value of formStepTouched when the value for step changes, so I am using useEffect. But useEffect throws a warning that its missing dependency of formStepTouched. As that is the value that's changing, putting that in the dependency array would cause infinite calls.
const [step, setStep] = useState(0);
const [formStepTouched, setFormStepTouched] = useState(
Array(childrenArray.length)
.fill(false)
.map((_, idx) => idx === 0)
)
useEffect(() => {
const newFormStepTouched = [...formStepTouched];
newFormStepTouched[step] = true;
setFormStepTouched(newFormStepTouched);
}, [step]);
Please refer to the codesandbox link below: https://codesandbox.io/s/brave-gould-ymjt0?file=/src/App.js
As you can see the demo works perfectly however an error message is shown. If the dependecy is added useEffect will be called infinitely.
How to get rid of the error message.