Exploring the realm of functional programming has brought me to the concept of closures, but I find myself struggling to fully grasp it. I've created a state closure resembling one used in React, I am aware of the correct solution to make the code snippet function as expected, however, the underlying theoretical principles remain elusive to me.
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 is the modification not taking place?