ISPEAKCODE.COM

TALK · REACT MEETUP #91 · 31 MAY 2025

Unlocking simpler state management in React

Local state sprawl, prop drilling, Redux boilerplate and Context re-renders, and how Zustand replaces them with one hook. Every example is updated for Zustand v5, and the re-render lab lets you see the difference.

43 SLIDES ZUSTAND V5 RE-RENDER LAB
Jump to the re-render lab
Nabil at a Scaler School of Technology lectern with a laptop; beside him the projector shows the React Bangalore title slide 'Unlocking Simpler State Management in React'.
On stage at React Meetup #91
Event poster: React Bangalore, React Meetup #91, May 31, 2025, 2 to 6 PM IST, Scaler School of Technology. Speakers Nabil Nalakath, Sandeep Butte, Pratik Yadav, Vamshi Suram, Ajay BL and Jagadeeshwaran M.
The event poster
01

REACT MEETUP #91

Unlocking simpler state management in React

02

PART 1

The state-management struggle

03

Local state sprawl

Dozens of useState hooks across files.

04

Prop-drilling chaos

Passing setters through 3–4 component levels.

05

Redux boilerplate overhead

3 files just to update one value.

06

Context + useReducer → app-wide re-renders

Every consumer re-renders on any state change.

07

Redux Toolkit → steep learning and boilerplate

You still write the slice, connect and dispatch.

08Expanding brain meme: multiple useState hooks alone; prop-drilling setters 3 levels deep; Context + useReducer for global state; Zustand: one hook, no boilerplate.
09

PART 2

What is Zustand?

10

Tiny and unopinionated

About 1 KB gzipped. No Provider. Hook-based.

11

One create() call → your useStore hook

12

30% fewer re-renders moving from Context

13

Performance benchmarks

Subscribe + 1 update on 10k items

  • Context + useReducer2.1 ms
  • Redux Toolkit1.4 ms
  • Zustand select0.3 ms

Figures as presented at the talk.

14

Bundle size, gzipped

What each library adds next to React

  • React~30 KB
  • Redux Toolkit~5 KB
  • MobX~3 KB
  • Zustand~1 KB

Figures as presented at the talk.

15

PART 3

Core API in 3 lines

16

One create, one hook

ZUSTAND V5
import { create } from 'zustand'

const useStore = create(set => ({
  count: 0,
  inc:   () => set(s => ({ count: s.count + 1 }))
}))

UPDATED SINCE THE TALKThe slide used import create from 'zustand'. Zustand v5 dropped default exports, so it’s now import { create } from 'zustand'.

17

PART 4

Redux vs. Zustand

18
ReduxZustand
BoilerplateHighMinimal
ProviderRequiredNone
Bundle (gzip)~5 KB~1 KB
19'Is this a pigeon?' meme: an 'old Redux user' points at a butterfly labelled '1 KB Zustand hook' and asks 'Is this too good to be true?'
20

PART 5

Counter example: Redux vs. Zustand

21

Redux Toolkit

4 FILES + PROVIDER
// counterSlice.ts
import { createSlice } from '@reduxjs/toolkit'

// 1. Define slice with initial state + reducer
const counterSlice = createSlice({
  name: 'counter',
  initialState: 0,
  reducers: {
    inc: (state) => state + 1
  }
})

// 2. Export action creator and reducer
export const { inc } = counterSlice.actions
export default counterSlice.reducer
22

useSelector + useDispatch

3 files + Provider, before the component even renders.

23

Zustand

2 FILES · NO PROVIDER
// store.ts (4 lines)
import { create } from 'zustand'

export const useStore = create((set) => ({
  count: 0,
  inc: () => set((s) => ({ count: s.count + 1 })),
}))
24

One useStore, zero slice/store wiring

25Drake meme: rejecting 'Writing slice.ts, store.ts, index.tsx, Counter.tsx', approving 'Just import useStore() and go!'
26

PART 6

Using the store

Selecting, updating and optimising state access

27

Select state

ZUSTAND V5
// store.ts
import { create } from 'zustand'

export const useStore = create(set => ({
  user: {
    name: { first: 'Alice', last: 'Doe' },
    email: 'alice@example.com',
    preferences: { theme: 'dark', notifications: true }
  },
  // updater functions omitted
}))
28

Subscribe to just one field

ZUSTAND V5
// Subscribe only to `user.name.first`
const firstName = useStore(state => state.user.name.first)

return <div>User First Name: {firstName}</div>
29

Update state

ZUSTAND V5
// store.ts
import { create } from 'zustand'

export const useStore = create(set => ({
  user: {
    name: { first: 'Alice', last: 'Doe' },
    preferences: { theme: 'light', notifications: true }
  },
  setTheme: (newTheme: string) =>
    set(state => ({
      user: {
        ...state.user,
        preferences: { ...state.user.preferences, theme: newTheme }
      }
    }))
}))
30

Call the updater

ZUSTAND V5
// Get updater for nested piece of state
const setTheme = useStore(state => state.setTheme)

return (
  <button onClick={() => setTheme('dark')}>
    Switch to Dark Theme
  </button>
)
31

Combine selector and updater

ZUSTAND V5
import { useShallow } from 'zustand/react/shallow'

// Combined selector: read firstName + theme updater
const { firstName, setTheme, currentTheme } = useStore(
  useShallow(state => ({
    firstName:    state.user.name.first,
    setTheme:     state.setTheme,
    currentTheme: state.user.preferences.theme
  }))
)

return (
  <>
    <p>Welcome, {firstName}!</p>
    <p>Current Theme: {currentTheme}</p>
    <button onClick={() => setTheme('dark')}>Dark Mode</button>
  </>
)

UPDATED SINCE THE TALKThe slide passed shallow as a second argument to useStore. v5’s create no longer takes an equality function; wrap the selector in useShallow instead.

32

Why selectors?

  • Components re-render only when the state they selected changes.
  • Updates stay minimal and fast.
33

Who re-renders?

Four components share one store. Pick how they read it, then change something and watch who re-renders. A model of the rule each approach uses: Context re-renders every consumer; a Zustand selector re-renders only when what it returns changes (Object.is, or a shallow compare with useShallow).

HOW COMPONENTS READ STATE

CHANGE THE STATE

Change something to see who re-renders.

    34

    PART 7

    Slices and middleware

    35

    Modular stores: split state by feature

    ZUSTAND V5
    // todosStore.ts
    import { create } from 'zustand'
    
    export const useTodosStore = create(set => ({
      todos: [] as string[],
      add:   (todo: string) => set(s => ({ todos: [...s.todos, todo] }))
    }))
    
    // userStore.ts
    import { create } from 'zustand'
    
    export const useUserStore = create(set => ({
      user: null as User | null,
      login: (u: User) => set({ user: u })
    }))
    36

    Middleware composition

    ZUSTAND V5
    // store.ts
    import { devtools, persist } from 'zustand/middleware'
    import { create } from 'zustand'
    
    export const useStore = create(
      devtools(
        persist(
          set => ({
            count: 0,
            inc:   () => set(s => ({ count: s.count + 1 }))
          }),
          { name: 'counter-storage' }
        )
      )
    )
    37

    When to use middleware

    • PERSISTAuto-save state to localStorage.
    • DEVTOOLSTime-travel debugging.
    • IMMER, LOGGER, CUSTOMAs needed.
    38

    PART 8

    Tips and best practices

    39

    Use selectors for performance

    ZUSTAND V5
    // Subscribe only to what you need
    const count = useStore(state => state.count)
    
    return <div>Count: {count}</div>
    40
    • KEEP STORES FOCUSEDOne hook per feature domain.
    • PUSH COMPLEX LOGIC TO SERVICESThe store holds only state and simple updaters.
    • AVOID INLINE OBJECT SELECTORSThey make a new object every time, so the component re-renders every time.
    41

    Avoid inline object selectors

    ZUSTAND V5
    // ❌ Creates new object each call
    useStore(s => ({ count: s.count }))
    
    // ✅ Separate subscriptions
    const count = useStore(s => s.count)
    const inc   = useStore(s => s.inc)

    EVEN MORE TRUE IN V5An inline object selector used to cost a wasted re-render; in Zustand v5 it can loop forever. Split the subscriptions, or wrap the selector in useShallow.

    42

    Questions?

    43

    Thank you