Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 6 of 10
Rendering & the Virtual DOM
Understanding exactly when React renders-and why-is the difference between writing efficient components and accidentally creating performance problems. React re-renders are not DOM updates: they're cheap function calls. Knowing this helps you stop over-optimizing and focus on genuinely expensive operations.
When React Re-renders a Component
- 1. State changes - calling a state setter function (from useState or useReducer) always schedules a re-render of that component
- 2. Props change - when a parent re-renders, ALL children are re-rendered by default, regardless of whether their props changed
- 3. Context changes - any consumer of a Context re-renders when the context value changes
- 4. Parent re-renders - the default behavior: parent renders โ children render recursively
- Key insight: a re-render = React calling your function again to compute new JSX. It does NOT mean DOM updates - React diffs first and only commits actual changes
- React.memo / useMemo / useCallback are tools to skip re-renders - use them AFTER you've confirmed a performance issue, not preemptively
The Render & Commit Phases
// React's rendering has two phases:
// RENDER PHASE (pure, can be interrupted in React 18+):
// - React calls your component function
// - React builds the new Virtual DOM tree
// - React DIFFS new tree vs previous tree
// - React determines what changed (the "patch")
// COMMIT PHASE (synchronous, never interrupted):
// - React applies the patch to the real DOM
// - Browser paints the updated pixels
// - useEffect and useLayoutEffect callbacks run
// React StrictMode (development only!) runs render phase TWICE
// to help surface impure components:
<React.StrictMode>
<App /> {/* component functions called twice in dev */}
</React.StrictMode>
// This is why effects run twice in dev - it's intentional!
// If your component breaks with double invocation, it's impure.
// The key prop - tells React an element has a NEW identity
function UserProfile({ userId }: { userId: string }) {
return (
<ProfileForm
key={userId} // โ when userId changes, React UNMOUNTS old ProfileForm
/> // and MOUNTS a fresh one - resets all state
);
// Without key, React reuses the same ProfileForm and just updates props
}Lists & Keys - Correct Reconciliation
// keys help React identify which list items changed, moved, or were removed
// Keys must be: stable, unique among siblings, and not index-based for reorderable lists
interface Task {
id: string;
title: string;
done: boolean;
}
function TaskList({ tasks }: { tasks: Task[] }) {
return (
<ul>
{tasks.map((task) => (
// โ
Use a stable unique ID from the data
<li key={task.id}>
<input type="checkbox" checked={task.done} readOnly />
{task.title}
</li>
))}
</ul>
);
}
// โ NEVER use array index as key when the list can be reordered/filtered
tasks.map((task, index) => <li key={index}>...)
// When React reorders items, index keys make it update all items
// instead of moving them - massive performance issue + state bugs
// โ
USE index only for STATIC lists that never change order or count
const LABELS = ['First', 'Second', 'Third'];
LABELS.map((l, i) => <span key={i}>{l}</span>); // OK hereCommon Mistakes - Rendering
- Assuming re-render = slow: React re-renders are cheap function calls; the actual DOM update is what's expensive. Profile before optimizing
- Using array index as key for dynamic lists - causes incorrect diffs, state being associated with the wrong item, and animation bugs
- Creating functions/objects inside render without memoization when passing to memoized children - breaks the memoization because new references fail equality checks
- Triggering state updates during render - calling setX() directly in the function body (not in an event handler or useEffect) causes an infinite loop
- Over-relying on `key` to reset state - resetting state by changing `key` is a valid pattern, but overusing it causes unnecessary unmount/mount cycles; prefer a controlled reset function
Tip
Tip
Practice Rendering the Virtual DOM in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Rendering & the Virtual DOM
Practice Task
Note
Practice Task - (1) Write a working example of Rendering the Virtual DOM from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Rendering the Virtual DOM is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Key Takeaways
- Understanding exactly when React renders-and why-is the difference between writing efficient components and accidentally creating performance problems.
- 1. State changes - calling a state setter function (from useState or useReducer) always schedules a re-render of that component
- 2. Props change - when a parent re-renders, ALL children are re-rendered by default, regardless of whether their props changed
- 3. Context changes - any consumer of a Context re-renders when the context value changes
Finished this lesson?