I've always thought that having any form of setState
inside a useEffect
call would result in an infinite re-render loop due to the useEffect
being called on each render. But for some reason my code is fine and my Next.js application runs perfectly. Could I know what the gaps in my knowledge are?
const [base, setBase] = useState(DEFAULT_BASE)
const [rateData, setRateData] = useState([])
const [symbolList, setSymbolList] = useState([])
// At the start, we want to display the rates in terms of EUR
useEffect(async () => {
const data = await calculator(FETCH_DATA, base)
setRateData(data)
// Create the symbol list
const symbols = []
Object.keys(data).map((c) =>
symbols.push(c)
)
setSymbolList(symbols)
console.log(symbolList)
}, [])