I have a MongoDB with Express that serves the data on an endpoint, which is accessed by React's useEffect
:
function App() {
// const [responsive, setResponsive] = useState("vertical");
// const [tableBodyHeight, setTableBodyHeight] = useState("100%");
const [data, setData] = useState([]);
const [columns, setColumns] = useState([]);
// const options = {
// filter:true,
// filterType:'dropdown',
// responsive,
// }
// Fetch data / headers from express server
useEffect(() => {
const fetchData = async () => {
const resp = await fetch('http://localhost:4000/todos/');
const respData = await resp.json();
// Filter out irrelevant data
const keysToFilterOut = ['_id', '__v']
const firstDatum = respData[0];
const filteredDatum = _.omit(firstDatum, keysToFilterOut);
const filteredColumns = Object.keys(filteredDatum);
setData(respData);
setColumns(filteredColumns);
};
fetchData()
}, [data]);
The useEffect
hook is called all the time, which means the data
is somehow always changing. Indeed, I verified it by adding these 3 lines to the hook:
console.log(data === respData);
console.log(data);
console.log(respData);
and the 1st console log is indeed false
. I don't get it, since the server didn't change the data, and moreover, I looked at the 2 other console logs - they seem identical. How is it that the data is different and how to fix it?
Here is an example from the console log of my app: