Why does the code below end up in an infinite loop? I believe that the same data is brought by fetch and set it to starWarData
. So, the value in the dependency array should be the same between renders.
React.useEffect(function() {
fetch("https://swapi.dev/api/people/1")
.then(res => res.json())
.then(data => setStarWarsData(data))
}, [starWarsData])
return (
<div>
{console.log("render")}
<pre>{JSON.stringify(starWarsData, null, 2)}</pre>
<h2>The count is {count}</h2>
<button onClick={() => setCount(prevCount => prevCount + 1)}>Add</button>
</div>
)
Code sample: https://scrimba.com/scrim/co3f04cbe8393c8d93714e7cd