mediumFrontend

What is the difference between shadow DOM and virtual DOM?

43 views
01

Understand the problem

Candidates often confuse these two concepts because they both involve a representation of the DOM.

domarchitectureconcepts
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Virtual DOM (React) vs Shadow DOM (native)
// Virtual DOM — React diffs this in memory and patches the real DOM
export default function App() {
  return <p style={{ fontFamily: "system-ui", padding: 24 }}>React uses a virtual DOM.</p>;
}

// Shadow DOM — a browser API for encapsulation (no React involved):
//   const host = document.querySelector("#host");
//   const shadow = host.attachShadow({ mode: "open" });
//   shadow.innerHTML = "<style>p{color:red}</style><p>Scoped — styles can't leak out</p>";
// The <style> above only affects nodes INSIDE this shadow root.
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.