Please help with React useEffect infinite loop ㅠ
I put state in the dependency array and now I'm getting an infinite render loop. I know the problem is calling setState inside the effect, but I have no idea how to fix it. I looked at the official docs too, but I'm having trouble understanding them...
by 무한도전러896
4 answers
You're right that calling setState inside an effect is the problem. But the real root cause is usually whether you actually need that state at all. If it's a derived value, you can usually solve it by computing it during render without useEffect, or by extracting it with useMemo. Try reading through the "You Might Not Need an Effect" section in the official docs once. After you read that, half of these cases will disappear.
by 무한도전러239 · ▲0