State Management in React

Why?

Prop Drilling

(N.B. Do check your component structure before despairing)

State Loss when Un-mounting

Solution:

Have state separate to the components

React Context API

Advanced Context API

Multiple Providers & Consumers (needed by functional components)

Unstated: A Library Alternative

And now, the grandaddy of them all...

(Which gave us useReducer)

Redux Key Ideas

  • There is a store where data is held
    • (It is often broken down into parts/slices)
  • It is passed using context (provider)
  • Components hook into it to get
  • The store has a reducer which controls how it updates when it receives...
  • Actions - signals sent to the reducer to effect changes to a store
    • Usually a simple object with
      • type: required
      • payload (name convention): data needed to do the update
  • Dispatch - the way we send them

What came next...

Zustand Key Ideas

  • There is a store where data is held
    • (It is often broken down into parts/slices)
  • It is passed using context (provider) (can be used outside react!)
  • Components hook into it to get
  • The store has a reducer which controls how it updates when it receives...
  • Actions - signals sent to the reducer to effect changes to a store
    • Usually a simple object with
      • type (naming convention not required)
      • payload  (naming convention not required)
  • Dispatch - the way we send them

Jotai Key Ideas

React Query

  • IF you're just data fetching why do you need global state AT ALL?
  • "It's time to break up with your global state!" - Tanner Linsley
  • Introducing: React Query!!!

State Management In React

By James Sherry

State Management In React

  • 1,602