KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
⚛️

React

Topic Hub & Articles

React Intro

10 min

Recap Quiz

Gated

Milestone Assessment

React Getting Started

10 min

React ES6

10 min

React Render HTML

10 min

Recap Quiz

Gated

Milestone Assessment

React JSX

10 min

React Components

10 min

React Class Components

10 min

Recap Quiz

Gated

Milestone Assessment

React Props

10 min

React Events

10 min

React Conditionals

10 min

Recap Quiz

Gated

Milestone Assessment

React Lists

10 min

React Forms

10 min

React Router

10 min

Recap Quiz

Gated

Milestone Assessment

React Memo

10 min

React CSS Styling

10 min

React Sass Styling

10 min

Recap Quiz

Gated

Milestone Assessment

React Fragments

10 min

React Portals

10 min

React Profiler

10 min

Recap Quiz

Gated

Milestone Assessment

React Strict Mode

10 min

React Higher Order Components

10 min

React Context API

10 min

React Error Boundaries

10 min

What is a Hook

10 min

Recap Quiz

Gated

Milestone Assessment

useState

10 min

useEffect

10 min

useContext

10 min

Recap Quiz

Gated

Milestone Assessment

useRef

10 min

useReducer

10 min

useCallback

10 min

Recap Quiz

Gated

Milestone Assessment

useMemo

10 min

Custom Hooks

10 min

useLayoutEffect

10 min

Recap Quiz

Gated

Milestone Assessment

useImperativeHandle

10 min

useDebugValue

10 min

useDeferredValue

10 min

Recap Quiz

Gated

Milestone Assessment

useTransition

10 min

useId

10 min

Progress
0%

0 / 38 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
ReactReact Hooks
Lesson

useReducer

10 min reading
Free Course

React useReducer Hook: Centralizing Complex State Transitions

useReducer is a React Hook designed for components whose state involves multiple related values or clear action-based transitions. Instead of scattering five setState setters across event handlers, you write one pure (state, action) => newState reducer function and call dispatch({ type: '...' }) to trigger updates.

Building a Shopping Cart Reducer

A reducer takes the current state and an action object, then returns the next state without mutating the original object. Grouping updates inside a switch statement makes business logic easy to read and unit-test outside React.

import { useReducer } from 'react';

function cartReducer(state, action) {
  switch (action.type) {
    case 'ADD_ITEM': {
      const existing = state.items.find((i) => i.id === action.item.id);
      if (existing) {
        return {
          items: state.items.map((i) =>
            i.id === action.item.id ? { ...i, qty: i.qty + 1 } : i
          ),
        };
      }
      return { items: [...state.items, { ...action.item, qty: 1 }] };
    }
    case 'CLEAR_CART':
      return { items: [] };
    default:
      return state;
  }
}

export function CartPanel() {
  const [state, dispatch] = useReducer(cartReducer, { items: [] });

  return (
    <section>
      <p>Cart items: {state.items.length}</p>
      <button
        type="button"
        onClick={() => dispatch({ type: 'ADD_ITEM', item: { id: 1, name: 'Keyboard' } })}
      >
        Add Keyboard
      </button>
      <button type="button" onClick={() => dispatch({ type: 'CLEAR_CART' })}>
        Clear
      </button>
    </section>
  );
}

When to Choose useReducer Over useState

Reach for useState for simple booleans, counters, or single form strings. Switch to useReducer when one user action updates three or more interdependent fields (like loading, data, error, and page) or when passing dispatch down to deeply nested children via Context.

Common useReducer Mistakes

  • Mutating state Inside a Case Block: Running state.items.push(...) and returning state mutates existing state in place, causing React to skip re-rendering. Always return a brand-new object or array.
  • Triggering Side Effects Inside the Reducer: Reducers must stay pure. Never call fetch, localStorage, or random functions inside a reducer; run side effects in event handlers or useEffect.

Frequently Asked Questions

Does dispatch change identity between renders?

No. React guarantees that the dispatch function stays stable across renders, so you can safely omit it from useEffect dependency arrays.

Can I lazily initialize state with useReducer?

Yes. Pass an optional third argument (useReducer(reducer, initialArg, initFn)), and React calls initFn(initialArg) once on mount.

Can useReducer work with TypeScript discriminated unions?

Yes. Typing action as a discriminated union gives autocomplete and exhaustive checks inside your switch statement.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Related Developer Tools & Topic Hub

React Topic Hub
JSON Formatter
Validate and format JSON payloads
HTML Formatter
Format JSX/HTML markup
CSS Formatter
Clean and format stylesheets
All React Articles
Deep-dive guides, tips & discussions
PrevioususeRef
Continue JourneyNext: useCallback

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum