I think I understand why React.StrictMode
causes functions to be called twice. However, I have a useEffect
that loads data from my api:
useEffect(() => {
async function fetchData() {
const data = await getData();
setData(data);
}
fetchData();
}, []);
In my getData()
function I call a maintenance script cullRecords()
that cleans up my data by deleting records over a certain age before returning the data:
async function getData(){
let results = await apiCall();
cullRecords(results);
return results;
}
Here's the rub: React.StrictMode
fires the getData()
function twice, loading up the apiCall()
twice and firing the cullRecords()
twice. However, by the time the second cullRecords()
subscript fires, my API throws an error because those records are already gone.
While it's not the end of the world, I'm curious if I'm doing something wrong, or if this is just a fringe case, and not to worry about it.