CodShot
TSX Public

Typed Context Reducer Pattern

Combine React context and useReducer with typed actions and a safe custom hook.

#react #context #reducer #typescript
TSX
'use client';

import {
  createContext,
  type ReactNode,
  useContext,
  useReducer,
} from 'react';

type CartState = {
  itemCount: number;
};

type CartAction =
  | { type: 'add'; quantity: number }
  | { type: 'clear' };

const CartContext = createContext<{
  state: CartState;
  dispatch: React.Dispatch<CartAction>;
} | null>(null);

function cartReducer(
  state: CartState,
  action: CartAction,
): CartState {
  switch (action.type) {
    case 'add':
      return {
        itemCount: state.itemCount + action.quantity,
      };

    case 'clear':
      return { itemCount: 0 };
  }
}

export function CartProvider({ children }: { children: ReactNode }) {
  const [state, dispatch] = useReducer(cartReducer, {
    itemCount: 0,
  });

  return (
    <CartContext.Provider value={{ state, dispatch }}>
      {children}
    </CartContext.Provider>
  );
}

export function useCart() {
  const context = useContext(CartContext);

  if (!context) {
    throw new Error('useCart must be used inside CartProvider.');
  }

  return context;
}

Snippet actions

CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support