← Back to React Course | Chapter 2: JSX & Rendering Fundamentals | Lesson 8 of 10

Virtual DOM की समझ

Virtual DOM React का page का rough sketch है जो यह memory में रखता है, ताकि यह असली page update करने का fastest तरीका पता लगा सके।

Virtual DOM क्या है

Virtual DOM असली DOM का एक lightweight JavaScript object representation है जो React memory में रखता है। जब state बदलता है, React एक नया virtual DOM tree बनाता है और इसे पिछले वाले से compare करता है असली DOM को immediately touch करने के बजाय।

Note: आप कभी directly virtual DOM बनाते या manage नहीं करते -- यह एक internal detail है जो React आपके लिए handle करता है।
Warning: Virtual DOM literally हर चीज़ में real DOM से faster नहीं है -- इसका benefit React जो real, expensive DOM operations perform करता है उनकी संख्या minimize करना है।

उदाहरण: What the Virtual DOM Is

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 VDomNote() {
  return <p>React compares virtual DOM trees before touching the real DOM.</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<VDomNote />);
  </script>
</body>
</html>

Diffing और Reconciliation

Reconciliation वह process है जहां React नए virtual DOM tree को पिछले वाले के against compare (diff) करता है और इसे match करने के लिए ज़रूरी minimal set of real DOM changes calculate करता है।

Note: यह diffing process वजह है कि state के एक piece को बदलना सिर्फ affected specific DOM nodes को update करता है, पूरे page को नहीं।
Warning: Reconciliation magic नहीं है -- एक list को deeply restructure करना (सिर्फ values नहीं, element types बदलना) React को expected से ज़्यादा DOM tear down और rebuild करने पर मजबूर कर सकता है अगर keys correctly उपयोग न हों।

उदाहरण: Diffing and Reconciliation

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 Ticker() {
  const [count, setCount] = React.useState(0);
  React.useEffect(() => {
    const id = setInterval(() => setCount((c) => c + 1), 1000);
    return () => clearInterval(id);
  }, []);
  return <p>Only this number updates in the real DOM: {count}</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<Ticker />);
  </script>
</body>
</html>

Virtual DOM Fast क्यों है

असली DOM को directly बदलना slow है क्योंकि browser को हर change के बाद layout recalculate और page repaint करना पड़ता है।

React पहले कई छोटे changes को एक single lightweight virtual DOM comparison में batch करके इससे बचता है, फिर सिर्फ ज़रूरी minimal set of real DOM updates apply करता है। यही वजह है कि UI के एक छोटे part के लिए state update करना पूरे page को redraw करने पर मजबूर नहीं करता।

Note: आपको virtual DOM के बारे में शायद ही कभी directly सोचना पड़ता है -- यह React का internal optimization है, कोई ऐसी चीज़ नहीं जिसके against आप code लिखते हैं।
Warning: Virtual DOM updates को efficient बनाता है, लेकिन यह React updates को instant नहीं बनाता -- एक component के अंदर expensive render logic अब भी चीज़ों को slow कर सकती है।

उदाहरण: Why the Virtual DOM Is Fast

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);
      return (
        <div>
          <p>Count: {count}</p>
          <button onClick={() => setCount(count + 1)}>+1</button>
        </div>
      );
    }
    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. यह सोचना कि virtual DOM हर update को instant बना देता है -- यह DOM work minimize करता है, लेकिन expensive JavaScript logic अब भी slow हो सकती है।
  2. यह मान लेना कि आपको अपने code में directly virtual DOM के साथ interact करना चाहिए -- आप कभी नहीं करते।
  3. 'virtual DOM' को असली browser DOM के साथ confuse करना वे अलग representations हैं।
चैप्टर सारांश
  • Virtual DOM UI का एक in-memory representation है जो React maintain करता है।
  • React changes ढूंढने के लिए नए virtual DOM को पिछले वाले के against compare (diff) करता है।
  • सिर्फ minimal necessary real DOM updates apply होते हैं, जिसे reconciliation कहा जाता है।
  • यह approach slower असली DOM को बार-बार touch करने की performance cost से बचाता है।
ब्राउज़र सपोर्ट

कोई browser-specific restrictions नहीं -- हर React version में available एक internal React implementation detail।

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.