I seem to have an infinite loop in my code but I can't seem to see where, I usually see this if I am setting the state of something that is a dependency of useEffect but here the two variables (values / items) are completely seperate.
App.js
import React from 'react'; import './style.css'; import MyComponent from './MyComponent.js'; export default function App() { return ( <div> <MyComponent /> </div> ); } MyComponent.js
import React, { useEffect, useState } from 'react'; const MyComponent = ({ values = [] }) => { console.log('MyComponent Reloaded'); const [items, setItems] = useState(); useEffect(() => { const loadItems = () => { //setItems([]); //why does this cause infinite render loop? }; loadItems(); }, [values]); return <></>; }; export default MyComponent; Why does this cause a render loop?
I have an example build Here (Uncomment the commented line to begin render loop)
useEffectruns when you changevalues. You changevaluesin thatuseEffect.valuesin each render. Remember that[] !== []souseEffectdetect this a achange