App-wide State Tools

Choose and use a shared-state tool for your stack: Zustand or Redux Toolkit, Riverpod or Bloc, ViewModel with StateFlow, or Observable.

Intermediate · about 8 hours · difficulty 3/5

Learn first: State Fundamentals

Once several screens share data, you want a predictable place to keep it. Each stack has a few popular options; pick one and learn it well rather than sampling all of them.

By stack

  • React Native: React Context for rarely changing values (theme, auth); Zustand for simple global stores; Redux Toolkit for large teams wanting strict patterns and devtools. Use TanStack Query for server data instead of putting API responses in a store.
  • Flutter: Riverpod (recommended modern choice), Provider, or Bloc/Cubit for event-driven, very testable flows.
  • Android: ViewModel exposing StateFlow<UiState>, collected in Compose with collectAsStateWithLifecycle().
  • iOS: @Observable classes (Observation framework) injected with @Environment or passed down.

What good looks like

  • Screens read state and dispatch intentions (addToCart(id)), never mutate the store directly from the UI.
  • Business logic lives in the store/ViewModel, which makes it unit-testable without UI.
  • Subscriptions are granular so one change does not re-render the whole app.

Pitfalls

  • Context holding fast-changing values re-renders every consumer.
  • Duplicating server data in a global store and then fighting to keep it fresh.

Good enough to move on: your cart logic lives in one store/ViewModel with unit tests, and screens are thin.

Key concepts

  • React Context: For low-frequency values
  • Zustand: React Native default pick
  • Redux Toolkit
  • Riverpod or Bloc: Flutter
  • ViewModel and StateFlow: Android
  • Observable and Environment: SwiftUI
  • Keeping business logic out of views
  • Avoiding unnecessary re-renders

Mini project

Refactor your cart app so all cart logic lives in a store or ViewModel, add persistence of the cart, and write three unit tests for it.

Check yourself

Why should API responses usually live in a query cache rather than a global store like Redux or Zustand?

Free resources