TechnicalEasy React
What is the virtual DOM and how does React update the UI?
Asked for Frontend Developer, Full-Stack Developer
Why they ask this: Checks that you understand what React does for you rather than only using its syntax.
Answer
The virtual DOM is a lightweight JavaScript description of the UI (React elements). When state or props change:
- React re-renders the affected components, producing a new element tree.
- It compares the new tree with the previous one. This is called reconciliation or diffing.
- It applies only the minimal real DOM changes needed in the commit phase.
Diffing rules worth knowing:
- Elements of a different type (
<div>vs<span>, or different components) are replaced entirely. - In lists,
keytells React which item is which, so it can reuse and move elements correctly.
Touching the real DOM is the slow part, so batching updates and changing only what differs keeps the UI fast and lets you write declarative code.
Can you explain this in your own words?