🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

Rendering

AI & DATA SCIENCE // rendering

Rendering is the process by which React calls a component's function to determine what should appear on screen, then updates the real DOM to match.

Syntax

root.render(<App />);

Deep Dive Course

React re-renders a component whenever its state changes, its props change, or its parent re-renders — during a render, React calls the component function to get its returned JSX, builds an updated virtual DOM representation, and diffs it against the previous version to figure out the minimal actual changes needed in the real DOM, a process it then commits during the separate commit phase. Rendering itself must be a pure operation, with no side effects like network calls or DOM manipulation happening directly in the component's function body — those are handled separately, in effects, precisely because a render can potentially happen more than once, or be discarded, before a final version is actually committed.

1Understanding Rendering

React re-renders a component whenever its state changes, its props change, or its parent re-renders — during a render, React calls the component function to get its returned JSX, builds an updated virtual DOM representation, and diffs it against the previous version to figure out the minimal actual changes needed in the real DOM, a process it then commits during the separate commit phase. Rendering itself must be a pure operation, with no side effects like network calls or DOM manipulation happening directly in the component's function body — those are handled separately, in effects, precisely because a render can potentially happen more than once, or be discarded, before a final version is actually committed.

💡

Keep a component's function body free of side effects like fetching data or manipulating the DOM directly — those belong inside useEffect, since render itself needs to remain a pure function of props and state that's safe to call multiple times.

editor.html
function Clock({ time }) {
  console.log('Rendering Clock with time:', time);
  return <p>Current time: {time}</p>;
}
localhost:3000

2Practical Example

Here is a real-world application of Rendering showing how it is used in production React code.

editor.html
import { useState } from 'react';

function App() {
  const [count, setCount] = useState(0);
  console.log('App rendered');
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Rendering:

1. Keep component functions pure, computing their returned JSX from props/state only, with no side effects performed directly during rendering

2. Move side effects like data fetching, subscriptions, or manual DOM manipulation into useEffect, which runs after rendering and committing, not during it

3. Avoid creating new object/array/function values unnecessarily inside a render if they're passed as props to a memoized child, since a new reference on every render defeats memoization

⚠️

Tip: Keep a component's function body free of side effects like fetching data or manipulating the DOM directly — those belong inside useEffect, since render itself needs to remain a pure function of props and state that's safe to call multiple times.

editor.html
function Clock({ time }) {
  console.log('Rendering Clock with time:', time);
  return <p>Current time: {time}</p>;
}
localhost:3000

Examples

Example 01Basic Usage
function Clock({ time }) {
  console.log('Rendering Clock with time:', time);
  return <p>Current time: {time}</p>;
}
Example 02Advanced Example
import { useState } from 'react';

function App() {
  const [count, setCount] = useState(0);
  console.log('App rendered');
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}

Best Practices

  • Keep component functions pure, computing their returned JSX from props/state only, with no side effects performed directly during rendering
  • Move side effects like data fetching, subscriptions, or manual DOM manipulation into useEffect, which runs after rendering and committing, not during it
  • Avoid creating new object/array/function values unnecessarily inside a render if they're passed as props to a memoized child, since a new reference on every render defeats memoization

Interview Question

Why must a component's render function body avoid side effects like data fetching, rather than performing them directly while rendering?

Hint: Think about how many times, and under what circumstances, React might actually call a component function during a single logical update.

React doesn't guarantee that calling a component's function corresponds exactly one-to-one with a final, committed UI update — under certain conditions, particularly with features like Strict Mode in development, or concurrent rendering features that can start, pause, and discard a render before committing it, React may call a component function more than once, or call it and never actually commit that particular render's result at all. If a component performed a genuine side effect, like firing a network request or writing to localStorage, directly inside its function body, that side effect would fire every time the function is called for rendering purposes, potentially multiple times or for renders that get thrown away entirely, causing duplicate or entirely unnecessary side effects. useEffect exists specifically to separate side effects from the pure rendering calculation, running only after a render has actually been committed to the real DOM, and only as many times as its dependency array indicates it genuinely needs to.

Exercises

MediumPractice using Rendering in a real scenario.
View Solution
function Clock({ time }) {
  console.log('Rendering Clock with time:', time);
  return <p>Current time: {time}</p>;
}

Frequently Asked Questions

Why must a component's render function body avoid side effects like data fetching, rather than performing them directly while rendering?

React doesn't guarantee that calling a component's function corresponds exactly one-to-one with a final, committed UI update — under certain conditions, particularly with features like Strict Mode in development, or concurrent rendering features that can start, pause, and discard a render before committing it, React may call a component function more than once, or call it and never actually commit that particular render's result at all. If a component performed a genuine side effect, like firing a network request or writing to localStorage, directly inside its function body, that side effect would fire every time the function is called for rendering purposes, potentially multiple times or for renders that get thrown away entirely, causing duplicate or entirely unnecessary side effects. useEffect exists specifically to separate side effects from the pure rendering calculation, running only after a render has actually been committed to the real DOM, and only as many times as its dependency array indicates it genuinely needs to.

Related Functions

stateuseeffectcomponents