I'm working wtih a requirement where I have to send to the server whatever a user types in a form (even if he doesn't submit it).
Before hooks I would use componentWillUnmount and grab all the state variables and send it to the backend, easy peasy.
Now with hooks things changed and this is where my struggles start. I know that I can achieve the ComponentWillUnmount with this:
useEffect(() => {
return () => {
//the code for unmount
}
}, []);
However this is not as simple as I need to have a variable in the useeffect "[]" or i will run into stale closure problem as describbed here Why can't useEffect access my state variable in a return statement? . What this means is that if I leave it as "[]" my state variables will be null.
My question is how would it be a clean solution? I don't have a variable that I know that it will necessarily change in the particular form, so I don't have a good variable to put on the "[]".
I thought that maybe I would create a "dirty" variable that if at least one field changed it would set it to true and I could use this variable for the [] in the useEffect, but this seems like a bit of work for something that should be much simpler.
Does anyone has a clue of an elegant solution for this?