Virtual DOM vs Shadow DOM: what is the difference?

IntermediateReact interview questionBy , updated 21 November 2026

Short answer

They solve different problems and only share part of a name. The virtual DOM is a JavaScript data structure that React builds on every render and compares with the previous one to update the real DOM with minimal changes. The Shadow DOM is a browser standard that attaches a hidden, separate DOM tree to an element, so its markup and styles are isolated from the rest of the page. You can use both at once.

One sentence each

  • Virtual DOM: a library technique for updating the DOM efficiently. It lives in your JavaScript memory, the browser knows nothing about it, and it disappears if you stop using React.
  • Shadow DOM: a platform feature for encapsulating part of the DOM. It is real DOM, built into the browser, and works with or without any framework.

The virtual DOM answers "what changed since the last render?". The Shadow DOM answers "how do I stop the page's CSS and scripts from reaching into my component, and mine from leaking out?".

Side-by-side comparison

Virtual DOM Shadow DOM
What it is Plain JavaScript objects describing the UI A real DOM subtree attached to a host element
Who provides it React (and similar libraries) The browser (a web standard)
Problem it solves Turning state changes into minimal DOM writes Isolating markup, styles and IDs
Visible in DevTools Elements panel No Yes, as #shadow-root
Affects CSS scoping No Yes: outside selectors do not match inside, inside styles do not leak out
Affects events No Yes: events crossing the boundary are retargeted to the host
Created by Rendering JSX element.attachShadow({ mode: "open" })

The virtual DOM in short

When state changes, React calls your components again, gets a new tree of elements, diffs it against the last committed tree and applies only the differences in the commit phase. This is reconciliation. It does not isolate anything: a React component's markup sits in the same document as everything else, and a global button { color: red } rule still styles its buttons. The full picture is in what the virtual DOM is.

The Shadow DOM in short

class PriceTag extends HTMLElement {
  connectedCallback() {
    const root = this.attachShadow({ mode: "open" });
    root.innerHTML = `
      <style>span { color: green; font-weight: 600; }</style>
      <span><slot></slot></span>
    `;
  }
}
customElements.define("price-tag", PriceTag);

<price-tag>$20</price-tag> now renders a green span, and the span rule inside the shadow root does not touch any other span on the page. Page styles do not match the inner span either, although inherited properties such as color and font-family and CSS custom properties still flow in. That is how design systems expose theming hooks. The Shadow DOM is one of the pieces of web components, together with custom elements, templates and slots.

Using them together

React can render a web component like any other tag, and React 19 supports custom elements fully: it sets properties instead of only attributes when the element defines them, and you can listen to custom events with on props.

React can also render inside a shadow root, which is a common way to ship a widget whose styles cannot clash with the host page:

import { createRoot } from "react-dom/client";

const host = document.getElementById("chat-widget");
const shadow = host.attachShadow({ mode: "open" });
createRoot(shadow).render(<ChatWidget />);

React keeps using its virtual DOM to update the nodes inside the shadow root. Since React 17, event listeners are attached to the root container rather than to document, so events inside a shadow root reach React's handlers. Stylesheets must be injected into the shadow root itself, because global CSS no longer applies there.

What interviewers listen for

  • That the two are not alternatives. One is about update performance and developer ergonomics, the other about encapsulation.
  • That the virtual DOM is not a browser feature and is not inherently faster than direct DOM writes.
  • That the Shadow DOM changes how CSS and events behave, and knowing what still crosses the boundary: inherited properties, custom properties, ::part() and events that are composed.

Common follow-up questions

Do Vue or Angular use the Shadow DOM?

Vue uses a virtual DOM, like React. Angular does not use a virtual DOM. Angular components can opt into the Shadow DOM with ViewEncapsulation.ShadowDom; by default Angular emulates style scoping with generated attributes. Vue's <style scoped> also emulates scoping rather than using a shadow root.

Is CSS Modules the same as the Shadow DOM?

No. CSS Modules rewrite class names at build time so they do not collide. They do not stop a global selector like div p from matching your markup. The Shadow DOM isolates in the browser, at run time.

What is the difference between open and closed shadow roots?

With mode: "open", outside code can reach the tree through element.shadowRoot. With "closed", that property returns null. Closed mode is not a security boundary; it only discourages access. Practise the boundary rules by predicting events across a shadow DOM boundary.

Can I build both myself?

Mistakes to avoid

  • Saying React uses the Shadow DOM

    It does not; it renders into the normal document unless you give it a shadow root.

  • Describing the Shadow DOM as a faster DOM

    It has nothing to do with update speed.

  • Expecting global CSS, such as a reset or Tailwind utilities, to style elements inside a shadow root

  • Calling document.querySelector to find nodes inside a shadow root

    Query the shadow root instead.

  • Treating a closed shadow root as a way to hide secrets from scripts on the page

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Implement a minimal React-like renderer with virtual DOM diffing and keyed reconciliationHard75 minFree
  2. Build a web component with shadow DOM, slots and attribute and property syncMedium40 min
  3. Predict events across a shadow DOM boundary: retargeting, composed and composedPath()Easy25 min
  4. Parse a JSX-like string into a virtual DOM tree and render it back to escaped HTMLHard50 min
  5. Write closest() and matches() polyfills that work in detached trees and shadow rootsEasy20 min