Why doesn't this function work like the useState() hook from react?
const myState = val => {
let value = {
val,
set setVal(newVal) {
this.val = newVal;
}
};
const setValue = newVal => {
value.setVal = newVal;
}
return [value.val, setValue];
}
const [myVal, setMyVal] = myState("initial value");
console.log(myVal);
setMyVal("updated value");
console.log(myVal);
Expected output:
initial value
updated value
Actual output:
initial value
initial value