第2引数のReferential Equality(参照の等価性)
コード
import { useEffect, useMemo, useState } from "react";
function App() {
const [count, setCount] = useState(0);
const [x, setX] = useState({ name: "X" });
const y = { name: "Y" };
const z = useMemo(() => {
return { name: "Z" };
}, []);
useEffect(() => {
console.log("effect x");
}, [x]);
useEffect(() => {
console.log("effect y");
}, [y]);
useEffect(() => {
console.log("effect y");
}, [z]);
return (
<div className="App">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
</div>
);
}
export default App;