I am trying to wrap my head around functional programming and one of the main concepts is closures - but I think there is something more that I am lacking and I cannot put things together. This is a state closure that I wrote similar to the one in React, I know the right answer to make the code snippet work, I just don't understand because of what theoretical facets this does not work.
const useState = function () {
let state: maze = {
size: 0,
};
const setState = (newState) => {
state = { ...newState };
};
const myStateObj = {
state,
setState,
};
return myStateObj;
};
const handleMazeSize = (e: InputEvent) => {
const newMaze: maze = {
size: Number((e.target as HTMLInputElement).value),
};
console.log(useState().state);
console.log(useState().setState(newMaze));
console.log(useState().state); // still size 0, expected the inputted size
};
Why does it not get modified?