← Back to React Course | Chapter 4: State & Event Handling | Lesson 2 of 10

State को सही तरीके से Update करना

State को correctly update करने का मतलब है हमेशा React से change करने के लिए कहना, खुद sneak in कर के value change करने के बजाय।
Syntax
markup
const [state, setState] = useState(initialValue);

// wrong: state = newValue;
setState(newValue);         // new value appears on the next render

State को कभी Directly Mutate न करें

React partly यह check करके decide करता है कि re-render करना है या नहीं कि state value change हुई या नहीं। अगर आप existing value को जगह पर mutate करें एक नई बनाने के बजाय, React शायद change detect न करे और UI update न हो, भले ही underlying data actually change हुई हो।

Note: State update करते समय हमेशा एक नई value बनाएं (एक नया object, array, या primitive), existing वाली modify करने के बजाय।
Warning: state.push(newItem) के बाद setState(state) जैसा कुछ अक्सर re-render करने में fail होता है क्योंकि array reference change नहीं हुई।

उदाहरण: Never Mutate State Directly

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 Toggle() {
      const [isOn, setIsOn] = React.useState(false);
      return (
        <button onClick={() => setIsOn(!isOn)}>
          {isOn ? "ON" : "OFF"}
        </button>
      );
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<Toggle />);
  </script>
</body>
</html>

State Updates Instant नहीं होते

एक state setter call करना next render के लिए एक update schedule करता है -- यह currently-running function के अंदर immediately variable की value change नहीं करता। अगर आप setter call करने के ठीक बाद state variable log करें, आप अब भी old value देखेंगे।

Note: अगर आपको नई value पर immediately act करना है, इसे एक separate local variable में compute करें, state variable पर rely करने के बजाय।
Warning: setCount(count + 1); console.log(count); जैसा code OLD count log करेगा, नया नहीं -- यह कई beginners को surprise करता है।

उदाहरण: State Updates Are Not Instant

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 Demo() {
      const [count, setCount] = React.useState(0);
      function handleClick() {
        setCount(count + 1);
        // count here is still the OLD value until the next render
      }
      return <button onClick={handleClick}>Count: {count}</button>;
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<Demo />);
  </script>
</body>
</html>

State Renders के बीच Persist करता है

एक normal local variable के विपरीत जो हर बार function चलने पर reset होता है, useState से declared state React द्वारा same component instance के re-renders भर में preserve किया जाता है।

यही exactly है जो एक component को समय के साथ एक counter value जैसी चीज़ें remember करने देता है।

Note: State को function के बाहर रहने वाली चीज़ की तरह सोचें, एक जगह जो React manage करता है, भले ही आप इसे function body के अंदर declare करें।
Warning: एक component के अंदर declared एक plain let variable हर render पर अपनी initial value पर reset हो जाता है -- सिर्फ useState (या similar Hooks) actually persist करता है।

उदाहरण: State Persists Between Renders

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 Counter() {
      const [count, setCount] = React.useState(0);
      // A plain variable here would reset to 0 every render; state does not.
      return <button onClick={() => setCount(count + 1)}>{count}</button>;
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<Counter />);
  </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. state में एक object या array को एक नई copy बनाने के बजाय directly mutate करना।
  2. same function में इसका setter call करने के ठीक बाद immediately state variable की value पर rely करना।
  3. यह भूल जाना कि previous render से state updates वही हैं जो आप next render होने तक देखते हैं।
चैप्टर सारांश
  • State को हमेशा इसके setter function के through update किया जाना चाहिए, कभी directly mutate नहीं।
  • React re-render करना है या नहीं decide करने के लिए old और new state compare करता है।
  • State updates next render पर effect लेते हैं, instantly नहीं।
  • State को immutable मानना React apps में subtle bugs रोकता है।
ब्राउज़र सपोर्ट

कोई browser-specific restrictions नहीं -- React 16.8 से useState पर applies होता है।

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.