07. Use Case Scenario 1: Zustand

Re-rendering optimization with zustand, handling structured data

πŸ”– 1. What is Zustand?

Key Features

  • A lightweight state management library that allows simple and intuitive global state management in React applications

  • You can define and use global state with just one or two hooks and simple function calls without complex setup

  • When creating a store, you simply define state and actions that change state in object form

  • It subscribes only to necessary state and re-renders only when that state changes, reducing unnecessary rendering

  • Asynchronous logic (API communication, data fetching, etc.) can be easily included in state definition logic, making it convenient to handle side effects

Example Code

Adapted from code on Zustand official documentation main pagearrow-up-right

import create from 'zustand';

// 1. Define global state (store)
// Create a store (useStore) by defining state and actions (increase, decrease, reset) with create function
const useStore = create((set) => ({
  count: 0,
  increase: () => set((state) => ({ count: state.count + 1 })),
  decrease: () => set((state) => ({ count: state.count - 1 })),
  reset: () => set({ count: 0 }),
}));

// When count value is updated through set, the state change function
// only components that depend on count re-render

function Counter() {
  // 2. Use state
  // When useStore hook is called within a component, you get count and each function
  const { count, increase, decrease, reset } = useStore();

  return (
    <div>
      <h1>{count}</h1>
      <button onClick={increase}>A bear is born!!</button>
      <button onClick={decrease}>A bear dies..</button>
      <button onClick={reset}>Reset</button>
    </div>
  );
}

export default function App() {
  return (
    <div>
      <h2>Zustand Bear 🐻</h2>
      <Counter />
    </div>
  );
}

Should I Use Zustand?

  • As such, Zustand defines a store and gets necessary state and functions through hooks

  • Since global state is defined and managed at once, state change logic can be gathered in one place, so maintenance is expected to be easy even as scale grows

  • Therefore, if you want to clearly structure global state at once and simplify state management flow through one store, Zustand would be good

  • Jotai requires breaking down state into atom units from the beginning, so I'm not sure if it can be used well in the early stages of a project

πŸ”– 2. Immutable State Model

Explaining 'Immutable State Model'

  • When managing state, Zustand updates state by creating a new object with only the changed parts instead of modifying the original state object

  • In other words, when state changes, instead of modifying the existing object, it creates a new object and puts that object in place of the existing state

  • And unchanged parts (e.g., other fields or internal objects) reuse the previous ones as they are, reducing unnecessary rendering

  • To use an analogy, instead of erasing content written on paper with an eraser and writing again, you write only the changed content on new paper and replace it. Unchanged content can reuse previous content without wasting resources

Example Code

Feeling the Benefits Through Examples

  • As such, Zustand makes it easy to understand how state has changed

  • By utilizing the immutability pattern that returns new objects, the process of comparing previous state and new state becomes simple

  • When the book says "Since you only need to check the equality of references to state objects to know if there's a change, you don't need to check the entire value of objects," it means you can determine whether state has changed just by checking "Is it the same object or a different object?"

πŸ”– 3. Manual Rendering Optimization

What is Selector-Based Re-rendering Control?

  • It's a strategy to subscribe only to specific parts of state that components need and re-render components only when those parts actually change

  • This way, even if other parts of global state change, components are not affected, reducing unnecessary re-rendering

  • Components that use only a very small part of large global state will only render when that small part changes, which will be advantageous for performance optimization

Example Code

My Thoughts

This chapter provides a practical introduction to Zustand, showing how it simplifies global state management compared to more complex solutions like Redux. The key insight is that Zustand provides a good balance between simplicity and functionality.

The discussion about immutable state models is particularly important - it shows how Zustand leverages React's optimization patterns to provide better performance without requiring complex setup.

The selector-based optimization examples demonstrate how Zustand can be used efficiently in real applications, showing that you can get good performance without over-engineering.

Code Examples

Last updated