Thuta Learning
React
IntermediateWeb Developmentintermediate

State Architecture with useReducer

Reactသင်ခန်းစာ 14

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • State Architecture with useReducer ရဲ့ render, state နဲ့ interaction behavior ကိုရှင်းပြနိုင်ရန်
  • Loading, error, accessibility နဲ့ lifecycle cases စမ်းနိုင်ရန်
  • Maintainable, testable နဲ့ performant React code ရေးနိုင်ရန်

State Architecture with useReducer မှာ state တစ်ခုချင်းစီ၏ owner, source of truth, update event နဲ့ derived value ကိုရှင်းလင်းစွာခွဲပါ။ State ကိုတိုက်ရိုက်မပြောင်းဘဲ immutable update လုပ်ပြီး user intent ကို event handler သို့ reducer action ဖြင့်ဖော်ပြပါ။

ပိုပြီးနားလည်ထားရမယ့် အချက်

State Architecture with useReducer မှာ state တစ်ခုချင်းစီ၏ owner, source of truth, update event နဲ့ derived value ကိုရှင်းလင်းစွာခွဲပါ။ State ကိုတိုက်ရိုက်မပြောင်းဘဲ immutable update လုပ်ပြီး user intent ကို event handler သို့ reducer action ဖြင့်ဖော်ပြပါ။

လက်တွေ့မှာ ဘယ်လိုအသုံးချမလဲ

State Architecture with useReducer example ကို empty/loading/error states, keyboard-only interaction, slow network, rapid repeated events နဲ့ component unmount/remount cases ဖြင့်စမ်းပါ။ Browser console, React DevTools, tests နဲ့ profiler ကိုလိုအပ်သလိုအသုံးပြုပြီး behavior ကိုအတည်ပြုပါ။

ဒီသင်ခန်းစာပြီးရင်

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
Reducer က increment နဲ့ reset user actions များကို သီးခြားကိုင်တွယ်မည်။

ကိုယ်တိုင်စမ်းကြည့်ရန်

State Architecture with useReducer example ကို empty/loading/error states, keyboard-only interaction, slow network, rapid repeated events နဲ့ component unmount/remount cases ဖြင့်စမ်းပါ။ Browser console, React DevTools, tests နဲ့ profiler ကိုလိုအပ်သလိုအသုံးပြုပြီး behavior ကိုအတည်ပြုပါ။

State/Effect သတိပေးချက်

Happy path တစ်ခု render မှန်ရုံဖြင့် stale state, repeated effects, accessibility, loading နဲ့ error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

Extracting State Logic into a ReducerReact

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Happy path တစ်ခု render မှန်ရုံဖြင့် stale state, repeated effects, accessibility, loading နဲ့ error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
  • Props/state ကိုတိုက်ရိုက်ပြောင်းခြင်း၊ derived state ထပ်သိမ်းခြင်း သို့ external system မရှိဘဲ useEffect ဖြင့် data flow ကိုရှုပ်ထွေးစေခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

State Architecture with useReducer example ကို empty/loading/error states, keyboard-only interaction, slow network, rapid repeated events နဲ့ component unmount/remount cases ဖြင့်စမ်းပါ။ Browser console, React DevTools, tests နဲ့ profiler ကိုလိုအပ်သလိုအသုံးပြုပြီး behavior ကိုအတည်ပြုပါ။

You'll know it worked when: Reducer က increment နဲ့ reset user actions များကို သီးခြားကိုင်တွယ်မည်။

State Architecture with useReducer | Thuta Learning