Overview
Understand how React works internally to build better, more performant applications. Learn about the virtual DOM, reconciliation algorithm, when components re-render, and how to optimize performance.Virtual DOM
React’s in-memory representation of UI
Reconciliation
How React updates the DOM efficiently
Component Re-rendering
When and why components update
Performance
Optimization techniques and tools
How React Updates the DOM
Why Virtual DOM?Direct DOM manipulation is slow. React’s virtual DOM:
- Batches multiple updates together
- Calculates minimum changes needed
- Updates real DOM only once
- Results in better performance
When Components Re-render
A component re-renders when:State Changes
useState or useReducer state updatesProps Change
Parent passes different props
Context Changes
Context value updates
Parent Re-renders
Parent component re-renders by default
Preventing Unnecessary Re-renders with memo
UseReact.memo() to prevent component re-renders when props haven’t changed.
- Without memo
- With memo
CounterOutput.jsx
value is the same.Complete Example
Counter.jsx
How memo Works
How memo Works
memo performs a shallow comparison of props:- If props are the same, skip re-render
- If props changed, re-render component
- Compares using
Object.is()by default
When to Use memo
When to Use memo
Use
memo when:- Component renders often with same props
- Component is expensive to render
- Component is pure (same props = same output)
memo itself has cost.The useMemo Hook
Memoize expensive calculations so they only re-run when dependencies change.useMemo Syntax
When to Use useMemo:
- Expensive calculations (complex algorithms)
- Creating objects/arrays passed as props to memoized children
- Referential equality matters for child components
- Simple calculations (overhead not worth it)
- Values that change frequently anyway
- As premature optimization
The useCallback Hook
Memoize function references to prevent child re-renders.Counter.jsx
- useMemo vs useCallback
- Why Functions Need Memoization
Keys and Component Identity
React uses keys to identify which elements changed, were added, or removed.Keys in Lists
Keys in Lists
- Identify which items changed
- Preserve component state
- Optimize re-renders
Keys for Component Reset
Keys for Component Reset
Changing a component’s key forces React to unmount and remount it:When
chosenCount changes, React:- Unmounts old Counter
- Mounts new Counter with fresh state
- Useful for resetting component state
React’s Rendering Process
Important Distinction:
- Rendering = Calling component function
- Committing = Updating the DOM
Performance Optimization Checklist
Use memo
Prevent re-renders when props unchanged
Use useMemo
Memoize expensive calculations
Use useCallback
Memoize function references
Proper Keys
Use stable unique keys in lists
Code Splitting
Lazy load components not immediately needed
Virtualization
Render only visible list items
Million.js Integration
The course also covers Million.js, a compiler that optimizes React applications:Million.js analyzes your components and applies optimizations automatically, potentially improving performance without manual memoization.
Common Performance Pitfalls
Creating Objects in Render
Creating Objects in Render
Inline Function Props
Inline Function Props
Heavy Computations in Render
Heavy Computations in Render
Profiling and Debugging
- React DevTools
- Why Did You Render
- Console Logging
Use React DevTools Profiler to:
- Record component render times
- Identify unnecessary re-renders
- Find performance bottlenecks
- Visualize component updates
Resources
React DevTools
Profile and debug React applications
React Reconciliation
Deep dive into React’s reconciliation