Here is the code snippet I am working with:
function change(initial) {
let a = initial;
console.log(a);
return [
a,
(v) => {
a = v;
}
];
}
const [val, setter] = change("initial");
console.log(val);
setter("s");
console.log(val);
Even though the setter function has been called, the value of 'val' remains the same. https://codesandbox.io/s/youthful-sun-ewqts?file=/src/index.js:0-212
If I modify the return statement to this instead:
return [
() => a,
(v) => {
a = v;
}
];
and then call val() as a function it works. I am curious to understand why this difference occurs.