I have a table that displays tableData. tableData is fetched and processed from a URL and might change (the data might change, not the URL) from time to time. I want to poll that URL, see if the data has changed, and if so, update the tableData using setTableData(<fetched and processed data>).
This is my take on it:
const [tableData, setTableData] = useState([]);
// Fetch data / headers from express server
useEffect(() => {
const fetchData = async () => {
const resp = await fetch('http://localhost:4000/todos/');
const respData = await resp.json();
setTableData(respData);
console.log('debug');
};
fetchData()
}, [tableData]);
Part of the function App return:
<MUIDataTable
title = { "Bla Bla" }
data = { tableData }
columns = { tableColumns }
options = { tableOptions }
/>
This useEffect is called continuously. It is my understanding that it works in the following way:
- Call once on the mount
- Call each update -> update the
tableDatawithsetTableData(respData); - Said data is rendered inside the
return
If I comment out the
set functions, theuseEffectis called once.If I use empty
Arraysin thereturn, theuseEffectis called all the time (Don't get it... I thought that since I'm using empty Arrays and not the data that is set in theuseEffect, therenderwill be called only once, and that will stop theuseEffectloops)
I want to just run the setTableData IFF the data fetched is different from the old data fetched, but nothing I tried doesn't cut it.