AI summary by TwinMind
Why Everyone Loves Zustand
Theo - t3․gg
29m 27s
Zustand Pronunciation and Introduction
- Author mispronounces "zustand" intentionally despite knowing correct German pronunciation
- Community favorite state management library for React
- Blog post by Veros about refactoring Teemo app to use zustand
What is Teemo App
- Sample application with 3 screens:
- Login/signin screen
- Time entries viewing/editing screen
- Timer screen for creating new entries
- Used for comparing different state management approaches
- Monorepo structure with separate versions for vanilla React, React Query, and zustand
Zustand Overview and API
- Small state management library providing React hooks
- Based on flux pattern like Redux but simplified further
- Package size: 3.1KB minified (44x smaller than React, 13x smaller than Redux)
- Simple API example:
import create from 'zustand' const useCountStore = create((set) => ({ count: 0, increment: () => set((state) => ({ count: state.count + 1 })) })) - Usage:
const { count, increment } = useCountStore()
Teemo Refactoring Process
- Refactoring took approximately 1 hour
- Almost drop-in replacement since existing code used hooks
- Main changes: replaced
useUserwithuseUserStore - Challenges with shared components in monorepo:
- Components using React context couldn't work with zustand version
- Had to refactor components to be pure
- Moved context-dependent components to separate folder
Side Effects and Initialization
- Challenge: Running actions when app starts (checking authentication via HTTP-only cookies)
- Author's solution: Call store action immediately after store creation
useUserStore.getState().fetchUser()
- Advantage: Request only made once (vs twice with useEffect in strict mode)
- Theo's alternative approach: Conditional store creation based on authentication state
Performance Benefits vs React Context
- Context issues:
- Any state change causes all subscribed components to render
- Context provider wraps entire app, causing full app rerenders
- Example: Saving user details caused entire app to render
- Zustand advantages:
- No provider wrapper required
- Updates sent directly to generated hooks
- Can subscribe to specific state values/actions
- Components only render when relevant state changes
Selector Pattern for Performance
- Use selectors to prevent unnecessary rerenders
- Example:
useCountStore(state => state.count)vsuseCountStore() - Component only updates when selected value changes
- Can perform calculations in selectors
- Function selectors (like
increment) never trigger rerenders since functions don't change
Async Operations
- Simple async handling: just call
setwith result - No middleware or promise chains required
- Example:
fetchUser: async () => { try { const user = await getUser() set({ user }) } catch (error) { set({ error }) } }
Theo's Ping App Implementation
- Complex nested zustand stores pattern
- Conditional store creation based on authentication/permissions
- External vanilla JS logic updating stores via
store.setState() - Examples:
- Real-time chat store with Ably integration
- Agora video call store with device management
- Stores only created when prerequisites met
Context Rendering Issue Investigation
- Theo investigates Teemo's context rerendering problem
- Issue:
useUserhook returns entire context object - Profile component needs only
clearUserfunction but gets all user data - Every user state change triggers profile rerender
- Zustand solution:
useUserStore(state => state.clearUser)selects only needed function
Final Assessment
- Thumbs up from Theo
- Lightweight, well-thought-out, approachable
- Good fit for Redux/flux pattern familiarity
- Next post will cover atomic patterns (Jotai)
- Both Jotai and zustand from Poimandres collective
- Poimandres also created React Three Fiber, React Spring, use-gesture
