Skip to content

State Management Strategies

At the end of this class, you should be able to…

  • Use TanStack Query’s invalidation and refetching mechanisms to synchronize client state with the server.
  • Implement optimistic updates for smoother user experiences.
  • Extend TanStack Query usage with advanced caching scenarios and error recovery workflows.

Additional Notes:

  • Encourage students to explore the TanStack Query Devtools to observe invalidation and refetch behaviour.
  • React 19’s concurrent features improve async rendering and may further enhance server-state synchronization performance.

This lesson consolidates everything we’ve learned about state and introduces a decision framework for choosing the right tool.

We will:

  • review types of state
  • map tools to state types
  • analyze the NAIT resources application
  • identify common mistakes
  • apply a decision framework to real scenarios

So far, we have used multiple approaches to manage state:

  • useState for local UI state
  • Context for shared client state
  • TanStack Query for server state
  • React Router for navigation and route parameters

This leads to an important question:

Which tool should we use for a given problem?

  • owned by a single component
  • short-lived
  • example: form inputs, toggles
  • shared across multiple components
  • UI-related
  • example: theme, selected item
  • comes from an API
  • can change outside your app
  • requires fetching, caching, synchronization

Map each state type to a tool:

  • useState -> local UI state
  • Context -> shared client state
  • TanStack Query -> server state
  • React Router -> navigation and route params

Key idea:

Different problems require different tools

Let’s categorize state in the existing application.

  • filters (e.g., virtualOnly toggle)
  • theme options (if shared across components)
  • resource list
  • selected resource details
  • current route
  • resourceId parameter

Ask these questions:

  1. Does this data come from an API? -> Use TanStack Query

  2. Is this shared UI state? -> Use Context

  3. Is this local to a component? -> Use useState

  4. Is this part of navigation? -> Use React Router

  • duplicates data
  • becomes stale
  • breaks caching
  • unnecessary complexity
  • performance issues

Mistake 3: Duplicating server state locally

Section titled “Mistake 3: Duplicating server state locally”
  • multiple sources of truth
  • loaders + queries + local state for the same data

Key idea:

One source of truth per type of data

Phase 6: Clean architecture for this project

Section titled “Phase 6: Clean architecture for this project”

Your application should now follow this structure:

  • React Router

    • handles routes and params
  • TanStack Query

    • handles all server data (queries and mutations)
  • Context

    • handles shared UI state only
  • useState

    • handles local component state
  • shared UI state
  • solution: Context
  • server data
  • solution: TanStack Query
  • local or shared UI state
  • solution: useState or Context
  • local state
  • solution: useState

For each scenario, choose the correct tool.

  1. A search input field
  2. A logged-in user profile (UI only)
  3. A list of resources from an API
  4. A selected item in a list
  5. Data that must stay in sync with a server

Discuss your answers with a partner.

  • State has different categories
  • Each category requires a different tool
  • TanStack Query owns server data
  • Context owns shared UI state
  • Avoid multiple sources of truth

For the NAIT resources application:

  1. Identify all state used in the app.
  2. Categorize each as:
    • local state
    • shared client state
    • server state
  3. Confirm that each uses the correct tool.
  4. Refactor any incorrect usage.