Next.js Discord

Discord Forum

React state is not update even after calling setDetectStart

Answered
XotEmBotZ posted this in #help-forum
Open in Discord
Avatar
I have this piece of code
 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

Avatar
Answer
Avatar
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.
Avatar
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?
Avatar
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 example
It should work as you expect
Avatar
Ok Thanks