I am using the useEffect cleanup function to store my formik values in localStorage. The strange part is the values stored are stale initial values and not the current form values. Here is the code:
console.log(values)
React.useEffect(() => {
// do stuff
return function() {
console.log(values)
// store form data on unmount
localStorage.setItem("formValues", JSON.stringify(values))
};
}, []);
The values logged outside the useEffect function are current, the one logged (and stored) within the cleanup function are stale initial values. I though the function would be lazily evaluated so the values variable would have the current value. As a shot in the dark, I also tried using a getter function for the form values. But still the old stale values. What's happening here?