I have the following code:
type myTuple = [string, (value:string) => void]
function setState(v: string) : myTuple {
let myState:string = v;
return [
myState,
(a) => myState = a
]
}
const [val,setVal] = setState('value1');
console.log(val);
setVal('Change');
console.log(val);
I'm expecting val to be changed to "Change", but instead it remains "value1" after the second console.log.
However, I did find a way to make it work:
type myTuple = [() => string, (value:string) => void ]
function setState(v: string) : myTuple {
let myState:string = v;
return [
() => myState,
(a) => myState = a
]
}
const [val,setVal] = setState('value1');
console.log(val());
setVal('AAP');
console.log(val());
Why is this working?