Quantcast
Channel: User Jack - Stack Overflow
Viewing all articles
Browse latest Browse all 40

How do I prevent this infinite loop?

$
0
0

I need to pass foo in the arguments of useEffect so that it can use the existing array values. So I set the new values in this array but when I went to set the value, it'll call f() function again from the userEffect. How do I get out this infinit loop?

Imaginary code example goes something like this:

const [foo, setFoo] = useState<MyType>(...);const f = () => {    const baa = {...foo};    for(...) {        foo.arr[x] = y;    }    setFoo(foo);}useEffect(() => {    f();    g();    // ...}, [foo]);

Viewing all articles
Browse latest Browse all 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>