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

Global और Local State में अंतर

Local state आपके अपने pocket में रखा एक note जैसा है, जबकि global state school के bulletin board पर पिन किया एक notice जैसा है जिसे हर कोई देख और पढ़ सकता है।

Local State क्या count होता है

Local state पूरी तरह एक component के अंदर रहता है, useState या useReducer से बनाया जाता है, और उस component के unmount होने पर गायब हो जाता है। एक single input का current text, या एक dropdown open है या नहीं, classic examples हैं — उस component के बाहर किसी को जानने या care करने की ज़रूरत नहीं।

Note: अगर आप सोच ही नहीं सकते कि कोई और component इस value की ज़रूरत पड़ेगी, तो यह एक strong sign है कि इसे local रहना चाहिए।
Warning: Local state हर बार इसका component unmount और remount होने पर reset होता है — इस पर navigation के through persist करने के लिए निर्भर न रहें जब तक यह deliberate design न हो।

उदाहरण: What Counts as Local State

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 SearchBox() {
  const [text, setText] = React.useState("");
  return <input value={text} onChange={e => setText(e.target.value)} />;
}
ReactDOM.createRoot(document.getElementById('root')).render(<SearchBox />);
  </script>
</body>
</html>

Global जाने से पहले State को Lift Up करना

जब दो sibling components को same value share और sync में रहने की ज़रूरत होती है, तो state को पूरी तरह global जाने की ज़रूरत नहीं — इसे उनके closest common parent component तक move करना (और props के रूप में नीचे pass करना) अक्सर काफी है।

इसे 'state lifting up' कहा जाता है।

Note: State lift up करना natural first step है; सिर्फ Context/Redux/Zustand की ओर जाएं जब prop-passing कई layers में genuinely awkward बन जाए।
Warning: State को बहुत ज़्यादा ऊपर lift करना 'just in case' (पूरे app root तक) जब सिर्फ दो nearby siblings को इसकी ज़रूरत हो, बीच की हर चीज़ के through unnecessary prop-passing जोड़ता है।

उदाहरण: Lifting State Up Before Going Global

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 Parent() {
  const [value, setValue] = React.useState("");
  return (
    <div>
      <input value={value} onChange={e => setValue(e.target.value)} />
      <p>Sibling sees: {value}</p>
    </div>
  );
}
ReactDOM.createRoot(document.getElementById('root')).render(<Parent />);
  </script>
</body>
</html>

State को असल में Global होने की कब ज़रूरत है

State global management के लिए एक अच्छा candidate बन जाता है जब इसकी ज़रूरत tree की scattered unrelated branches में कई components को होती है — जैसे current logged-in user, app theme, या एक shopping cart जो product page और checkout page दोनों से accessible हो जो component hierarchy में एक-दूसरे से दूर हैं।

Note: एक useful test: अगर state lift up करने के लिए इसे पांच या ज़्यादा unrelated component layers से pass करना पड़ेगा, तो यह instead global जाने का एक sign है।
Warning: सिर्फ दो nearby components द्वारा उपयोग होने वाले state के लिए global जाना सिर्फ इसे lift up करने की तुलना में unnecessary indirection जोड़ता है।

उदाहरण: When State Truly Needs to Be Global

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">
// Global: current user needed by Header, Sidebar, and Checkout,
// which don't share a close common parent.
const UserContext = React.createContext(null);
function Header() {
  const user = React.useContext(UserContext);
  return <p>Logged in as: {user || "Guest"}</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(
  <UserContext.Provider value="Asha"><Header /></UserContext.Provider>
);
  </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. हर चीज़ को 'to be safe' global बनाना, जो components को isolation में reuse और reason about करना harder बनाता है।
  2. हर चीज़ को local बनाना जब दो sibling components को असल में same synchronized value चाहिए।
  3. यह न पहचानना कि state organically कई जगहों पर ज़रूरी हो गया है और इसे lift या shared state में move करना चाहिए।
चैप्टर सारांश
  • Local state useState/useReducer के through एक component के अंदर रहता है और इसके बाहर visible नहीं होता।
  • Global state app में कहीं से भी accessible होता है, Context, Redux, Zustand, या similar tools के through।
  • State lift up करना (इसे एक shared parent तक move करना) अक्सर true global state की ओर जाने से पहले काफी होता है।
  • सही default है state को जितना हो सके local रखना, इसका scope सिर्फ ज़रूरत पड़ने पर widen करना।
ब्राउज़र सपोर्ट

कोई React-version restriction नहीं — यह एक architectural pattern है, कोई specific API नहीं।

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.