React Reference
The Virtual DOM is a lightweight JavaScript representation of the actual DOM. React uses it to track changes in the UI. When a component's state changes, React creates a new virtual DOM tree and compares it with the previous one (diffing). Then it updates only the parts of the real DOM that changed, improving performance.
✅ Direct manipulation of the real DOM is slow
✅ React uses the VDOM to minimize DOM updates
✅ This makes apps faster and more efficient
1.First render → VDOM:
<h2>Counter: 0</h2>
2.You click the button → count
becomes 1
New VDOM:
<h2>Counter: 1</h2>
3.React compares old and new VDOM, finds the text
difference in <h2>
, and updates only that part in the real DOM.
✅ Fast
✅ Efficient
✅ Smooth UI
Controlled components have their state managed by React (via useState
or this.state
). Their values are updated through props or state. Uncontrolled components rely on the DOM to manage state, often using useRef
to access values.
Feature |
Controlled Component |
Uncontrolled Component |
---|---|---|
Data stored in |
React state |
DOM (via ref) |
React controls input? |
✅ Yes |
❌ No |
Easier to validate? |
✅ Yes |
❌ Harder |
Use case |
Most React forms |
Simple or legacy forms |
In a controlled component, the input's value is tied to React state. You control the value using useState
.
In an uncontrolled component, the input's value is stored in the DOM, and you access it via a ref.
Hook |
When it runs |
Use case |
---|---|---|
useEffect |
After render (painted on screen) |
Side effects like API calls |
useLayoutEffect |
After DOM mutations but before paint |
Measure DOM size/layout before painting |
useInsertionEffect |
Before DOM mutations (CSS only) |
Injecting styles (like Emotion, styled-components) |
useEffect
– Normal Side EffectsuseLayoutEffect
– Synchronous Side EffectsgetBoundingClientRect
)useInsertionEffect
– CSS Injection (⚠️ Rarely used)