My component uses the useEffect hook to watch for updates of a valve.
I have set a condition to prevent useEffect firing after mounting.
When the button is clicked, the value is updated from the initial state. Why does useEffect gets fired every time the button is clicked? The value is always the same so there's no update.
import React, { useState, useEffect } from "react"; function doSomething() { return [ { id: 1, prop1: "foo", prop2: "bar" }, ]; } export default function Choose() { const [tableData, setTableData] = useState([{}]) // runs after every render useEffect(()=> { if (Object.keys(tableData[0]).length > 0) { console.log("something changed") } }, [tableData]) return ( <div> <button type="button" onClick={(e)=> setTableData(doSomething())}>display</button> </div> ); }
[] !== []and{} !== {}. So every time doSomething is called, you indeed get a new value - even though all the properties are the same.