I have a singleton class in Daemon.ts
export default class Daemon {
private static instance : Daemon;
callback : (tstring : string)=>void;
t : number;
constructor (callback: (tstring : string)=>void){
this.data = data
this.callback = callback;
this.t = 0;
window.setInterval(this.tick.bind(this), 1000)
}
public static getInstance(callback: (tstring : string)=>void){
if(!Daemon.instance){ Daemon.instance = new Daemon(callback);}
return Daemon.instance;
}
tick(){
this.t = this.t + 1
this.callback(this.t.toString());
}
}
Then in a separate file, Timefeedback.tsx, I have:
const TimeFeedback = () => {
const [time, updateSimTime] = React.useState<string>("starting string");
const updateTime = (tString : string) => {
updateSimTime(tString);
console.log(`update time called, val: ${tString}`)
}
const daemon = Daemon.getInstance(updateTime.bind(this));
return (
<div>
{time}
</div>
);
};
What I expect to happen:
- the time state is updated on each tick() from the daemon.
What actually happens:
- the callback function
updateTimeis successfully called, and the console log prints the correct values. But the setState function updateTime() What happens is I get console logs fromTimeFeedback.tsxwith the expected value oftStringprinted out. But the setState functionsetSimTimenever gets called, so the text in the div remains "starting time". Why is this?
I have investigated, when calling print events inside of Daemon I get:
function() {
[native code]
}
I tried to remove the .bind(this) inside TimeFeedback.tsx, but nothing changes except the print instead says:
tString => {
setSimTime(tString);
console.log(`update time called, val: ${tString}`);
}
I also walked through with the debugger and updateSimTime does get called but it has no effect.
Why is that updateSimTime has no effect?
datain theconstructor?this.data = datacodesandbox.io/s/kind-julien-yzkgwj?file=/src/App.tsxupdateTime.bind(this)doesn't make sense in a function component because a function component doesn't have athislike a class component does.datafield was accidentally left in after I pasted it overDaemonis in a separate .ts file, it appears ifDaemonis in the same file as theTimeFeedbackit'll work just fine!