Thuta Learning
React
IntermediateWeb Developmentintermediate

State Architecture with useReducer

ReactLesson 14

What you'll walk away with

  • Explain the render, state, and interaction behavior of State Architecture with useReducer
  • Test loading, error, accessibility, and lifecycle cases
  • Write maintainable, testable, and performant React

For State Architecture with useReducer, identify the owner, source of truth, update event, and derived values. Keep updates immutable and express user intent through event handlers or reducer actions.

Build a Complete Mental Model

For State Architecture with useReducer, identify the owner, source of truth, update event, and derived values. Keep updates immutable and express user intent through event handlers or reducer actions.

Apply It in Production React

Test an original State Architecture with useReducer example with empty/loading/error states, keyboard-only interaction, a slow network, rapid repeated events, and component unmount/remount cases. Verify behavior with the browser console, React DevTools, tests, and profiler where relevant.

After This Lesson

jsx
import { useReducer } from 'react';

function reducer(state, action) {
  switch (action.type) {
    case 'increment': return { count: state.count + 1 };
    case 'reset': return { count: 0 };
    default: throw Error('Unknown action: ' + action.type);
  }
}

export default function Counter() {
  const [state, dispatch] = useReducer(reducer, { count: 0 });
  return <>
    <p>Count: {state.count}</p>
    <button onClick={() => dispatch({ type: 'increment' })}>Add</button>
    <button onClick={() => dispatch({ type: 'reset' })}>Reset</button>
  </>;
}
You should see
The reducer handles increment and reset as explicit user actions.

Try It Yourself

Test an original State Architecture with useReducer example with empty/loading/error states, keyboard-only interaction, a slow network, rapid repeated events, and component unmount/remount cases. Verify behavior with the browser console, React DevTools, tests, and profiler where relevant.

State and Effect Warning

Assuming one happy-path render proves every stale-state, repeated-effect, accessibility, loading, and error path.

Extracting State Logic into a ReducerReact

Easy traps

  • Assuming one happy-path render proves every stale-state, repeated-effect, accessibility, loading, and error path.
  • Mutating props or state, storing redundant derived state, or using an Effect to complicate data flow when no external system exists.

Hands-on Exercise

Test an original State Architecture with useReducer example with empty/loading/error states, keyboard-only interaction, a slow network, rapid repeated events, and component unmount/remount cases. Verify behavior with the browser console, React DevTools, tests, and profiler where relevant.

You'll know it worked when: The reducer handles increment and reset as explicit user actions.

State Architecture with useReducer | Thuta Learning