Question presented to candidate: "Shadow DOM and virtual DOM — are they related?"
What a strong answer should cover:
- No. They solve unrelated problems and the shared word is a coincidence of naming. Saying that first is the right move.
- Virtual DOM is a React concept: a lightweight tree of plain JavaScript objects describing what the UI should look like, which React diffs against the previous tree to compute minimal DOM updates. It never exists in the browser spec.
- Shadow DOM is a browser standard, part of Web Components: a genuinely separate DOM subtree attached to an element, with real encapsulation — its nodes are not reachable from the outer document and its styles do not leak in either direction.
- Virtual DOM is about efficient updates; shadow DOM is about isolation.
- A React element is not a DOM node — it is an object with
type,propsandkey. - They can coexist: you can render a React tree into a shadow root, and React will happily reconcile inside it. Event delegation needs care, since React attaches listeners at the root container.
- The honest nuance: virtual DOM is not inherently faster than direct DOM manipulation — it is faster than naive re-rendering, and it buys a declarative programming model.
Clarifying questions expected:
- "Do you mean the concepts, or are you asking whether React uses shadow DOM?" — React does not, by default.
Code / implementation expected: No. This is a definitional comparison.