MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
State Reducer Pattern in Advanced React0%
Study Materials
React.js for AdvancedProgress 0%
1.1Control Props and Inversion of Control Pattern1.2State Reducer Pattern in Advanced React1.3Prop Getters and Flexible Design Systems1.4Headless UI Architecture and Accessibility Primitives
HomeTutorialsReact.js for AdvancedState Reducer Pattern in Advanced React
Prop Getters and Flexible Design Systems

State Reducer Pattern in Advanced React

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. The Limits of Control Props

While Control Props give the consumer ultimate control over state, it is an "all-or-nothing" proposition:

  • In uncontrolled mode, the component does everything.
  • In controlled mode, the consumer must manage every single state transition manually.

What if a consumer likes 95% of a component's internal logic, but wants to intercept, modify, or block a specific state change?

For example:

  • A dropdown menu should close on select, except when holding the Shift key (multi-select).
  • A counter should increment freely, except it must never exceed 4 clicks without confirming a modal.

The State Reducer Pattern solves this problem by allowing the consumer to pass a custom reducer function into the component to intercept internal state transitions!

1Normal Transition:
2[Action Dispatched] ──► [Internal Reducer] ──► [New State]
3 
4State Reducer Pattern:
5[Action Dispatched] ──► [Internal Reducer calculates nextState]
6 │
7 ▼
8 [Consumer's custom stateReducer]
9 (Can modify, override, or block the update!)
10 │
11 ▼
12 [Final State Applied]

2. Implementing the State Reducer Pattern

Let's build an advanced Counter component that accepts a stateReducer:

1import React, { useReducer } from 'react';
2 
3// 1. Export Action Types so consumers can reference them
4export const actionTypes = {
5 increment: 'INCREMENT',
6 decrement: 'DECREMENT',
7 reset: 'RESET'
8};
9 
10// 2. Default internal reducer
11function defaultReducer(state, action) {
12 switch (action.type) {
13 case actionTypes.increment:
14 return { count: state.count + 1 };
15 case actionTypes.decrement:
16 return { count: state.count - 1 };
17 case actionTypes.reset:
18 return { count: 0 };
19 default:
20 throw new Error(`Unsupported action type: ${action.type}`);
21 }
22}
23 
24// 3. Component accepting a stateReducer prop
25export default function AdvancedCounter({
26 initialCount = 0,
27 stateReducer = (state, action) => action.changes // Default: accept all changes
28}) {
29 // Enhanced reducer passes proposed changes to consumer's stateReducer!
30 const enhancedReducer = (state, action) => {
31 // A. Compute what the internal component wants to do
32 const changes = defaultReducer(state, action);
33 
34 // B. Let the consumer intercept, inspect, and modify those changes!
35 return stateReducer(state, { ...action, changes });
36 };
37 
38 const [state, dispatch] = useReducer(enhancedReducer, { count: initialCount });
39 
40 return (
41 <div className="counter-box">
42 <h3>Count: {state.count}</h3>
43 <button onClick={() => dispatch({ type: actionTypes.decrement })}>-</button>
44 <button onClick={() => dispatch({ type: actionTypes.increment })}>+</button>
45 <button onClick={() => dispatch({ type: actionTypes.reset })}>Reset</button>
46 </div>
47 );
48}

3. Consumer Example: Enforcing Business Rules

Now, a consumer can intercept transitions cleanly without taking over the entire state:

1export default function RestrictedApp() {
2 // Custom reducer intercepts state transitions
3 const customReducer = (state, action) => {
4 // Rule: Prevent incrementing beyond 5!
5 if (action.type === actionTypes.increment && state.count >= 5) {
6 alert('Maximum threshold reached!');
7 return state; // Return existing state = BLOCK THE UPDATE!
8 }
9 
10 // Otherwise, accept the component's calculated changes
11 return action.changes;
12 };
13 
14 return <AdvancedCounter initialCount={0} stateReducer={customReducer} />;
15}

4. Architectural Superpowers of State Reducers

  • Inversion of Control: The consumer injects decision-making logic directly into the heart of the component's state machine.
  • Zero Duplicate State: The consumer doesn't need to maintain their own useState or copy state out of the component.
  • Composable Customizations: Different consumers can apply totally different business policies to the exact same UI component.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What problem does the State Reducer Pattern solve in advanced React architecture?
Question 2
What is passed to the consumer's stateReducer(state, action) function?
Question 3
How can a consumer veto or cancel a state transition inside their stateReducer?
Question 4
Why are actionTypes exported as a constant object in components implementing state reducers?
Question 5
How does the State Reducer Pattern compare to the Control Props pattern?
Next Lesson

Prop Getters and Flexible Design Systems

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Control Props and Inversion of Control PatternProp Getters and Flexible Design Systems

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext