Why does React invoke a component function for an identical state update in one case but not another?
I am trying to understand how React handles state updates when the new state is identical to the previous state. Consider the following React component: import { useState, useEffect } from 'react'; export default function App() { const [resource, setResource] = useState("user"); console.log("render"); useEffect(() => { console.log("resource changed"); }); return ( <> <button onClick={() => setResource("user")}>User</button> <button onClick={() => setResource("posts")}>Posts</button> <button onClick={() => setResource("admin")}>Admin</button> <h2>{resource}</h2> </> ); } Observed behavior When
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.