← Back to React Course | Chapter 9: State Management | Lesson 5 of 10

Store, Actions और Reducers

Actions और reducers एक restaurant kitchen में order tickets जैसे हैं — ticket (action) बताती है कि क्या order हुआ, और chef (reducer) ही सिर्फ dish actually prepare करने (state update करने) के लिए allowed है।
Syntax
markup
// Action: a plain object
{ type: "domain/actionName", payload: data }

// Reducer: (state, action) => newState
function reducer(state = initialState, action) {
  switch (action.type) {
    case "domain/actionName":
      return { ...state, key: action.payload };
    default:
      return state;
  }
}

एक Action की Anatomy

एक action बस एक plain JavaScript object है जिसमें, कम से कम, एक type field होता है — एक string जो describe करती है कि क्या हुआ, जैसे 'cart/addItem'।

वह action process करने के लिए ज़रूरी कोई भी additional data (जैसे कौन सा item जोड़ा गया) आमतौर पर एक payload field के रूप में include होता है।

Note: type strings के लिए एक common convention है 'domain/eventName', जैसे 'cart/addItem' या 'auth/login', unrelated actions के बीच collisions से बचने के लिए।
Warning: Actions सिर्फ describe करते हैं कि क्या हुआ — एक dispatch करना खुद कुछ नहीं बदलता जब तक एक reducer उसे handle न करे।

उदाहरण: Anatomy of an Action

markup
// Run in your local React project (npm install required)
const addItemAction = {
  type: 'cart/addItem',
  payload: { id: 1, name: 'Book' },
};
store.dispatch(addItemAction);

एक Reducer की Anatomy

एक reducer एक function है जो current state और एक action लेता है, और next state return करता है। यह pure होना चाहिए — same inputs दिए जाने पर, यह हमेशा same output produce करता है, कोई API calls नहीं, कोई mutations नहीं, और कोई randomness नहीं।

Note: यह decide करने के लिए कि कौन सी logic चलानी है, action.type पर एक switch statement (या एक if/else chain) उपयोग करें।
Warning: एक default case भूल जाना जो unchanged state return करे इसका मतलब है कि एक unrecognized action type undefined return कर सकता है, store को corrupt करते हुए।

उदाहरण: Anatomy of a Reducer

markup
// Run in your local React project (npm install required)
function cartReducer(state = { items: [] }, action) {
  switch (action.type) {
    case 'cart/addItem':
      return { items: [...state.items, action.payload] };
    default:
      return state;
  }
}

One-Way Data Flow

Redux एक strict cycle enforce करता है: एक component एक action dispatch करता है, reducer उससे नया state compute करता है, store update होता है, और वह state पढ़ने वाला कोई भी component नई value के साथ re-render होता है।

यह predictable, one-directional flow ही Redux apps को debug और reason about करने में आसान बनाता है।

Note: Redux DevTools आपको literally हर dispatched action और resulting state change देखने देता है, जो इस flow को debug करने के लिए invaluable है।
Warning: इस cycle में कुछ भी UI को directly update नहीं करता — components सिर्फ इसलिए re-render होते हैं क्योंकि वे useSelector के through store से subscribed हैं, state change पर react करते हुए।

उदाहरण: The One-Way Data Flow

markup
// Run in your local React project (npm install required)
// 1. Component dispatches:
dispatch({ type: 'cart/addItem', payload: { id: 2, name: 'Pen' } });
// 2. Reducer computes new state (shown above)
// 3. Store updates internally
// 4. useSelector(state => state.cart.items) re-runs in any subscribed component
// 5. That component re-renders with the new items array
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. दो अलग actions को एक जैसा type string देना, जो एक को accidentally दूसरे की logic trigger करने का कारण बनता है।
  2. Side effects (API calls, timers) को directly एक reducer के अंदर डालना, middleware या एक component में डालने के बजाय।
  3. Reducer के default case से undefined return करना, unchanged state return करने के बजाय।
चैप्टर सारांश
  • एक action एक plain object है जिसमें एक type field होता है जो describe करती है कि क्या हुआ, plus optional data (payload)।
  • एक reducer एक pure function है: (state, action) => newState, कोई side effects नहीं।
  • Store का dispatch(action) function state update trigger करने का इकलौता तरीका है।
  • Reducers को हमेशा एक value return करनी चाहिए, उन actions के लिए unchanged state भी शामिल जिन्हें वे नहीं पहचानते।
ब्राउज़र सपोर्ट

npm install redux (या @reduxjs/toolkit) चाहिए — इस sandbox में CDN से उपलब्ध नहीं है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.