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

सही State Management Solution चुनना

एक state-management tool चुनना leftovers के लिए सही size का container चुनने जैसा है — एक छोटे snack को एक huge box की ज़रूरत नहीं, और एक हफ्ते के meals एक छोटे में fit नहीं होते।

Local State: useState और useReducer

अगर एक piece of state सिर्फ एक component (या वह component और props के through इसके direct children) उपयोग करता है, तो plain useState या useReducer almost हमेशा सही, simplest choice है।

यहां एक global state library की ओर जाना बिना किसी real benefit के complexity जोड़ता है।

Note: हर नए piece of state को local state के रूप में शुरू करें, और इसे सिर्फ तब lift up करें या shared state management में move करें जब आपको असल में इसे share करना हो।
Warning: Local-only state को 'just in case' Context या Redux में डालना बिना matching benefit के indirection जोड़ता है।

उदाहरण: Local State: useState and useReducer

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
function App() {
  const [isOpen, setIsOpen] = React.useState(false);
  return <button onClick={() => setIsOpen(!isOpen)}>{isOpen ? "Close" : "Open"} Menu</button>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
  </script>
</body>
</html>

Shared State: Context, Zustand, या Redux

जब state को tree की अलग, unrelated branches में components द्वारा पढ़ना या update करना हो, तो आपको local state से आगे कुछ चाहिए। Context API React में built-in है और moderate cases के लिए fine है; Zustand/Jotai एक lighter-weight external option देते हैं; Redux सबसे ज़्यादा structure और tooling देता है, बड़े scale पर valuable।

Note: एक rough decision path: पहले Context try करें; अगर update frequency या app size इसे strain करने लगे, तो आगे Zustand या Redux Toolkit consider करें।
Warning: कोई single best choice नहीं है — सही tool team size, app complexity, और shared state असल में कितनी बार बदलता है इस पर depend करता है।

उदाहरण: Shared State: Context, Zustand, or Redux

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
    const ThemeContext = React.createContext("light");
    function ThemeLabel() {
      const theme = React.useContext(ThemeContext);
      return <p>Shared via Context (built-in, no install): {theme}</p>;
    }
    function App() {
      return <ThemeContext.Provider value="dark"><ThemeLabel /></ThemeContext.Provider>;
    }
    ReactDOM.createRoot(document.getElementById("root")).render(<App />);
  </script>
</body>
</html>

एक Practical Decision Checklist

Practice में: क्या सिर्फ एक component (या इसके direct children) को यह चाहिए? useState उपयोग करें। क्या tree में कुछ components को यह चाहिए, बिना constant rapid changes के?

Context fine है। क्या एक बड़े app को complex update logic, strong DevTools, और team-wide conventions के साथ यह चाहिए?

Redux Toolkit consider करें। बीच में कुछ minimal setup के साथ चाहिए? Zustand try करें।

Note:
  • एक app के लिए इनमें से कई साथ उपयोग करना पूरी तरह normal है:
  • widgets के लिए local useState
  • theme के लिए Context
  • core business data के लिए Redux।
Warning: इसे पूरे app के लिए एक one-time decision के रूप में treat न करें — state के अलग pieces reasonably अलग tools उपयोग कर सकते हैं।

उदाहरण: A Practical Decision Checklist

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
// A real app often mixes approaches:
// - useState: form input, modal open/closed
// - Context: theme, current logged-in user
// - Redux/Zustand: shopping cart, notifications, complex shared business data
function Example() {
  const [open, setOpen] = React.useState(false); // local
  return <p>{open ? "Open" : "Closed"}</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<Example />);
  </script>
</body>
</html>
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. हर project पर default रूप से Redux की ओर जाना, actual complexity चाहे जो भी हो।
  2. बहुत frequently-changing state (जैसे mouse position) के लिए Context उपयोग करना, जो performance issues का कारण बन सकता है क्योंकि हर change पर हर consumer re-render होता है।
  3. App बढ़ने के साथ choice को फिर से कभी न visit करना — एक tool जो शुरुआत में fine था वह बाद में bottleneck बन सकता है।
चैप्टर सारांश
  • एक component या एक छोटे group के लिए local state के लिए useState/useReducer काफी हैं।
  • Context API बिना frequent, rapid updates के moderate number of components में shared state के लिए suit करता है।
  • Redux (या Redux Toolkit) complex, frequently-changing, widely-shared state वाले बड़े apps और strong tooling/conventions की ज़रूरत के लिए suit करता है।
  • Zustand/Jotai/Recoil shared state के लिए Redux के lighter-weight alternatives देते हैं, कुछ structure को simplicity से trade करते हुए।
ब्राउज़र सपोर्ट

Comparison खुद के लिए कोई React-version restriction नहीं — हर tool की अपनी npm package requirements देखें।

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.