Home / Function/ reducer() — ui Function Reference

reducer() — ui Function Reference

Architecture documentation for the reducer() function in use-toast.ts from the ui codebase.

Entity Profile

Dependency Diagram

graph TD
  c07b5fcc_69de_7a7c_19e6_3382a66187bb["reducer()"]
  0ac37ef4_ee03_7234_1fa3_be05404864ce["use-toast.ts"]
  c07b5fcc_69de_7a7c_19e6_3382a66187bb -->|defined in| 0ac37ef4_ee03_7234_1fa3_be05404864ce
  4cadcb11_5cb9_1eed_a20f_6444ac24ea81["dispatch()"]
  4cadcb11_5cb9_1eed_a20f_6444ac24ea81 -->|calls| c07b5fcc_69de_7a7c_19e6_3382a66187bb
  ac6cc7b8_7ed9_9e4c_685d_fdc48030a64f["addToRemoveQueue()"]
  c07b5fcc_69de_7a7c_19e6_3382a66187bb -->|calls| ac6cc7b8_7ed9_9e4c_685d_fdc48030a64f
  style c07b5fcc_69de_7a7c_19e6_3382a66187bb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

deprecated/www/registry/default/hooks/use-toast.ts lines 77–130

export const reducer = (state: State, action: Action): State => {
  switch (action.type) {
    case "ADD_TOAST":
      return {
        ...state,
        toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
      }

    case "UPDATE_TOAST":
      return {
        ...state,
        toasts: state.toasts.map((t) =>
          t.id === action.toast.id ? { ...t, ...action.toast } : t
        ),
      }

    case "DISMISS_TOAST": {
      const { toastId } = action

      // ! Side effects ! - This could be extracted into a dismissToast() action,
      // but I'll keep it here for simplicity
      if (toastId) {
        addToRemoveQueue(toastId)
      } else {
        state.toasts.forEach((toast) => {
          addToRemoveQueue(toast.id)
        })
      }

      return {
        ...state,
        toasts: state.toasts.map((t) =>
          t.id === toastId || toastId === undefined
            ? {
                ...t,
                open: false,
              }
            : t
        ),
      }
    }
    case "REMOVE_TOAST":
      if (action.toastId === undefined) {
        return {
          ...state,
          toasts: [],
        }
      }
      return {
        ...state,
        toasts: state.toasts.filter((t) => t.id !== action.toastId),
      }
  }
}

Subdomains

Called By

Frequently Asked Questions

What does reducer() do?
reducer() is a function in the ui codebase, defined in deprecated/www/registry/default/hooks/use-toast.ts.
Where is reducer() defined?
reducer() is defined in deprecated/www/registry/default/hooks/use-toast.ts at line 77.
What does reducer() call?
reducer() calls 1 function(s): addToRemoveQueue.
What calls reducer()?
reducer() is called by 1 function(s): dispatch.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free