WhoBuilt

What State Management Library Should I Use with React?

Feb 8, 2021· 9 min· 54K views·Watch on YouTube

Lee Robinson, a React developer on the leerob channel, explains how to choose state management tools for React applications.

How React state management evolved

React made UI a function of state, but managing that state has long been challenging. Redux became popular through the Flux pattern and often handled both interactive UI state and data fetched from APIs.

Separate UI state from server data

UI state covers interactions such as modals and theme settings, while server cache state stores API responses for reuse. Robinson points to SWR and React Query as tools built for server data, a distinct problem from UI state. He also explains that Context transports data between components; paired with hooks such as useState or useReducer, it can support state management.

Choose tools for specific needs

State machines make possible states and transitions explicit; a stoplight, for example, moves from green to yellow to red. Robinson highlights XState’s visualizer for exploring transitions such as loading, success, failure, and retries. For complex interfaces such as Figma, performance may require specialized approaches, but he recommends starting with React or JavaScript and adding a library when a clear need arises.

Consider trade-offs and where state lives

Direct mutation can be convenient but carries risk, while immutable updates can require more boilerplate. Immer lets developers write changes against a draft and produces an immutable next state. Robinson also shows how URL state preserves filters across refreshes and makes them shareable, using Amazon and Nomad List as examples.

Pick based on the project

A large Context provider can cause many nested components to rerender when its value changes, so developers may use memoization to reduce unnecessary work. Robinson discusses a proposed useSelectedContext hook as a possible future optimization. His recommendations depend on project size, experience, and learning goals, with SWR or React Query suggested for server cache state.

Takeaways

  • Keep UI state and server cache state conceptually separate, and choose tools suited to each.
  • Start with React or JavaScript, then add a library when a specific need appears.
  • Use state machines when explicit transitions can clarify edge cases and failure paths.
  • Put shareable filters in the URL so they survive refreshes and can be shared.
  • Choose mutation and immutability patterns based on the safety and boilerplate trade-offs your team accepts.

Mentioned in this talk

Search inside this talk

What was claimed 6

  • feature
    Redux: Popularized a Flux-like approach to state management
    “popularized by a library called Redux”
    YouTube
    Unverified1 source· Talk
  • feature
    swr: Fetches API data and caches the response for use across the client
    “a library for doing that”
    YouTube
    Unverified1 source· Talk
  • feature
    TanStack Query: Manages server caching state
    “SWR and React Query, which allowed you to manage server caching state”
    YouTube
    Unverified1 source· Talk
  • feature
    Context Engine: Shares data between components; paired with hooks such as useState and useReducer, it can form a state management solution
    “just the means to transport your data between lots of different components”
    YouTube
    Unverified1 source· Talk
  • feature
    XState: Provides an online visualizer for viewing and trying out statecharts
    “They have a fancy visualizer online”
    YouTube
    Unverified1 source· Talk
  • feature
    Immer: Allows mutation-style code to produce an immutable result
    “write code in a mutable fashion that produces an immutable result”
    YouTube
    Unverified1 source· Talk

Related talks