React state is not update even after calling setDetectStart
Answered
XotEmBotZ posted this in #help-forum
I have this piece of code
the
const toggleDetect = () => {
const d = !detectStart
console.log("In toggel", d)
if (d) cameraObj.start()
if (!d) cameraObj.stop()
setDetectStart(d)
}
const startDetect = () => {
toggleDetect()
setTimeout(toggleDetect, 1 * 1000)
}
detect
and setDetectStart
is the use state var and its changing func.the
startDetect
is on a onClick of button but for some reason both time it runs, the detectStart is name, not inverted. What is going on?7 Replies
Check here: https://mantine.dev/hooks/use-timeout/
Answer
You don't have to install the library if you don't want, either go on the github and copy the source code or copy another implementation of the hook. Regardless, it should do what you want.
Ok I will look at the mantine project as a whole as i can see many things which can help me
though for now if I just want to get past this for presentation, what can I do?
Just do what you're currently doing, except you replace the part with the
setTimeout
code with the useTimeout
variation you saw in the linked exampleIt should work as you expect
Ok Thanks