React Native theorytheory 0/50 · 0%
State management · hard

41. State management at scale (Zustand/Redux)

Choosing and structuring app-wide state.

Beyond Context, dedicated state libraries like Zustand or Redux Toolkit offer better performance for frequently-updated global state (like live prices) via selective subscriptions instead of re-rendering the whole provider tree.

import { create } from "zustand";

interface WalletState {
  address: string | null;
  balance: number;
  setBalance: (b: number) => void;
}

const useWalletStore = create<WalletState>((set) => ({
  address: null,
  balance: 0,
  setBalance: (b) => set({ balance: b }),
}));

// component only re-renders when the balance field changes:
const balance = useWalletStore((s) => s.balance);

Choose based on team familiarity and needs: Zustand is minimal boilerplate for simpler apps; Redux Toolkit's middleware ecosystem (thunks, RTK Query) suits larger apps with complex async flows.

Check your understanding

  1. 1. What advantage do libraries like Zustand offer over plain Context for frequent updates?

  2. 2. When might Redux Toolkit be preferred?

  3. 3. In the Zustand example, when does a component re-render?