Hindsight β 13-13 May 16
How do you get around the fact that a setInterval completely screws up references to state when used in a useEffect? In this code,
console.log(frame)
always shows 0, but the state does update correctly in the render, and the setFrame works only when it's using a functional update...
17 Replies
(As you can see I tried adding
frame
to the setInterval arguments so it would maybe pass the value correctly but it doesn't work (and adding an argument to the function, it's also never updating)Unknown Userβ’3y ago
Message Not Public
Sign In & Join Server To View
"called" interval?
Unknown Userβ’3y ago
Message Not Public
Sign In & Join Server To View
setInterval starts the interval on its own...
Unknown Userβ’3y ago
Message Not Public
Sign In & Join Server To View
Yes, that was the issue I was trying to resolve without having to cleanup and re-do the interval every 50ms in this case
which clearly isn't possible with React
Unknown Userβ’3y ago
Message Not Public
Sign In & Join Server To View
Not without ac ustom hook
Unknown Userβ’3y ago
Message Not Public
Sign In & Join Server To View
If
frame
is in the dependency array then the interval gets recreated from scratch every 50ms which sounds like horrible optimisation
I did actually get some answers from https://discord.com/channels/102860784329052160/103696749012467712/975761012206735360
I'm not really happy with the answers but it's because React, not because of the people answering πUnknown Userβ’3y ago
Message Not Public
Sign In & Join Server To View
To be honest I'd rather use Solid... where you can predictably do this and it works:
Β―\_(γ)_/Β―
But thanks for trying to help, I just asked in many places and got various answers π
Unknown Userβ’3y ago
Message Not Public
Sign In & Join Server To View