Rendering is the process of React calling your components to figure out what should be displayed on the screen. Understanding how React renders helps you write more performant applications and debug rendering issues.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/facebook/react/llms.txt
Use this file to discover all available pages before exploring further.
The Rendering Process
When you render a React application, three things happen:- Triggering a render: Something causes React to render
- Rendering the component: React calls your component functions
- Committing to the DOM: React updates the actual DOM
Triggering a Render
There are two reasons for a component to render:Initial Render
When your app starts, React needs to display the initial UI:Re-renders (State Updates)
Once the component has been initially rendered, you can trigger additional renders by updating state:setCount:
- React schedules a re-render
- React calls your component function again
- React updates the DOM if needed
Updating a component’s state automatically queues a render. You can think of this like a restaurant guest ordering tea, dessert, and all sorts of things after putting in their first order, depending on the state of their thirst or hunger.
The Render Phase
During rendering, React calls your component to figure out what should be on screen.Initial Render
On initial render, React calls the root component:- Call
App() - Call
Header(),Main(), andFooter() - Call any nested components
- Continue recursively until there are no more nested components
Re-renders
On subsequent renders, React calls the component whose state update triggered the render:setCount is called:
- React re-renders
App - React re-renders
Counter(it receives new props) - React may skip
HeaderandFooterif their props haven’t changed
React Elements and the Virtual DOM
When you write JSX, it creates React elements - lightweight descriptions of what to render:Reconciliation: Comparing Renders
After rendering your components, React needs to figure out what changed. This process is called reconciliation. React compares the new element tree with the previous one:- React compares the new tree with the old tree
- It finds that only the text content of
<h1>changed - It marks only that text node for update
- The
div,h1element, andbuttondon’t need to change
Reconciliation Rules
Different Types: Replace the Tree
When elements have different types, React replaces the entire tree:- Unmount the old
<div>and its children - Destroy old
Countercomponent state - Mount the new
<span>and its children - Create new
Countercomponent with fresh state
Same Type: Update Props
When elements have the same type, React updates the props:- Keep the same DOM node
- Update only the changed attributes
Keys: Identifying Elements
When rendering lists, React uses keys to match children:The Commit Phase
After reconciliation, React knows which DOM nodes need to be added, updated, or removed. This is the commit phase.Committing Changes
React applies the minimal set of changes to the DOM:text changes from “Hello” to “Hi”:
- React doesn’t touch the DOM structure
- React only updates the text content of the
<p>node - This is much faster than recreating the DOM
Browser Paint
After React commits changes to the DOM, the browser repaints the screen:- Render phase: React calculates changes (can be interrupted)
- Commit phase: React applies changes to DOM (synchronous, cannot be interrupted)
- Browser paint: Browser updates the screen
In React, “rendering” refers to React calling your component, not the browser painting pixels. The browser paint happens after React’s commit phase.
Render and Commit Phases
The distinction between render and commit phases is important:- Render Phase
- Commit Phase
Characteristics:
- Pure computation
- Can be interrupted
- Can be run multiple times
- No side effects
- No DOM mutations
- React calls component functions
- Calculates what changed
- Builds element tree
useStateuseMemouseReducer- Render logic
Optimizing Renders
Prevent Unnecessary Re-renders
React re-renders a component when:- Its state changes
- Its parent re-renders
- Its context value changes
Batching State Updates
React automatically batches multiple state updates:Key Placement
You can use keys to reset component state:Concurrent Rendering
React 18 introduced concurrent rendering, which allows React to work on multiple state updates simultaneously and prioritize urgent updates:Debugging Renders
React DevTools
Use React DevTools to:- Highlight components when they render
- See why a component rendered
- Profile rendering performance
Console Logging
useEffect Dependencies
Missing dependencies cause extra renders:Summary
- Rendering = React calling your component functions
- Reconciliation = React comparing old and new element trees
- Committing = React updating the DOM
- React elements are immutable snapshots of the UI
- Keys help React identify elements across renders
- The render phase is pure; the commit phase can have side effects
- React batches updates and optimizes DOM changes
Next Steps
Components
Learn about component types and composition
Hooks Overview
Explore all React Hooks