Pros and cons of this approach and library
Let's discuss the pros and cons of using Zustand or any other libraries to implement this approach.
To recap, the following are the reading and writing states of Zustand:
- Reading state: This utilizes selector functions to optimize re-renders.
- Writing state: This is based on the immutable state model.
The key point is that React is based on object immutability for optimization. One example is useState
. React optimizes re-renders with object referential equality based on immutability. The following example illustrates this behavior:
const countObj = { value: 0 }; const Component = () => { const [count, setCount] = useState(countObj); const handleClick = () => { setCount(countObj); }; useEffect(() => { console.log("component updated"); }); return ( &...