🚀 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...

Component Rendering

AI & DATA SCIENCE // component-rendering

Component rendering describes how React decides when to call a component's function again and how the resulting JSX gets converted into actual DOM updates.

Syntax

function Component() {
  // runs again whenever state/props/context change
  return <div>...</div>;
}

Deep Dive Course

A component re-renders whenever its own state changes, whenever the props passed to it change, or whenever its parent re-renders for any reason, by default — that last part means a re-render can cascade to every child component in a subtree even if a specific child's own props didn't actually change, unless that child is specifically wrapped in React.memo() to opt out of unnecessary re-renders based on unchanged props. React batches multiple state updates that happen within the same event handler into a single re-render for efficiency, rather than re-rendering separately after each individual state update call.

1Understanding Component Rendering

A component re-renders whenever its own state changes, whenever the props passed to it change, or whenever its parent re-renders for any reason, by default — that last part means a re-render can cascade to every child component in a subtree even if a specific child's own props didn't actually change, unless that child is specifically wrapped in React.memo() to opt out of unnecessary re-renders based on unchanged props. React batches multiple state updates that happen within the same event handler into a single re-render for efficiency, rather than re-rendering separately after each individual state update call.

💡

Wrap a component in React.memo() when it's expensive to render and its parent re-renders often for unrelated reasons, so it only re-renders when its own props actually change — but don't reach for this optimization by default, since it adds its own overhead and complexity that isn't worth it for cheap, simple components.

editor.html
function Parent() {
  const [count, setCount] = useState(0);
  console.log('Parent rendered');
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <Child />
    </div>
  );
}

function Child() {
  console.log('Child rendered');
  return <p>I am a child.</p>;
}
localhost:3000

2Practical Example

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

editor.html
const Child = React.memo(function Child() {
  console.log('Child rendered');
  return <p>I am a child.</p>;
});
localhost:3000

3Best Practices

Follow these guidelines when working with Component Rendering:

1. Rely on React's default re-rendering behavior for most components, reaching for React.memo() only when a specific, genuinely expensive component's unnecessary re-renders are a measured performance problem

2. Remember that multiple state updates inside the same event handler are batched into a single re-render, rather than triggering a separate render for each one

3. Avoid creating new object, array, or function values inline as props to a memoized child on every render, since a new reference defeats React.memo()'s comparison even if the logical value is unchanged

⚠️

Tip: Wrap a component in React.memo() when it's expensive to render and its parent re-renders often for unrelated reasons, so it only re-renders when its own props actually change — but don't reach for this optimization by default, since it adds its own overhead and complexity that isn't worth it for cheap, simple components.

editor.html
function Parent() {
  const [count, setCount] = useState(0);
  console.log('Parent rendered');
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <Child />
    </div>
  );
}

function Child() {
  console.log('Child rendered');
  return <p>I am a child.</p>;
}
localhost:3000

Examples

Example 01Basic Usage
function Parent() {
  const [count, setCount] = useState(0);
  console.log('Parent rendered');
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <Child />
    </div>
  );
}

function Child() {
  console.log('Child rendered');
  return <p>I am a child.</p>;
}
Example 02Advanced Example
const Child = React.memo(function Child() {
  console.log('Child rendered');
  return <p>I am a child.</p>;
});

Best Practices

  • Rely on React's default re-rendering behavior for most components, reaching for React.memo() only when a specific, genuinely expensive component's unnecessary re-renders are a measured performance problem
  • Remember that multiple state updates inside the same event handler are batched into a single re-render, rather than triggering a separate render for each one
  • Avoid creating new object, array, or function values inline as props to a memoized child on every render, since a new reference defeats React.memo()'s comparison even if the logical value is unchanged

Interview Question

Why does a parent component re-rendering also cause its child components to re-render by default, even if the child's own props haven't actually changed?

Hint: Think about how React decides which components to re-render — is it purely based on that component's own props changing, or something broader?

By default, React's re-rendering behavior cascades downward through the component tree: whenever a parent component re-renders, React by default also re-renders every child in that subtree, regardless of whether that specific child's props actually changed, simply because rendering a parent means calling its function again, which includes evaluating the JSX for its children too. This default behavior favors simplicity and correctness over performance, since checking every single child's props for changes before deciding whether to skip its render would itself add overhead, and for most components that overhead of an unnecessary re-render is genuinely negligible anyway. React.memo() exists specifically to opt a particular component out of this default cascading behavior, telling React to skip re-rendering that component, and its own subtree, unless its props have genuinely changed, which is worth doing selectively for components that are either expensive to render or re-render very frequently due to an unrelated parent update.

Exercises

MediumPractice using Component Rendering in a real scenario.
View Solution
function Parent() {
  const [count, setCount] = useState(0);
  console.log('Parent rendered');
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <Child />
    </div>
  );
}

function Child() {
  console.log('Child rendered');
  return <p>I am a child.</p>;
}

Frequently Asked Questions

Why does a parent component re-rendering also cause its child components to re-render by default, even if the child's own props haven't actually changed?

By default, React's re-rendering behavior cascades downward through the component tree: whenever a parent component re-renders, React by default also re-renders every child in that subtree, regardless of whether that specific child's props actually changed, simply because rendering a parent means calling its function again, which includes evaluating the JSX for its children too. This default behavior favors simplicity and correctness over performance, since checking every single child's props for changes before deciding whether to skip its render would itself add overhead, and for most components that overhead of an unnecessary re-render is genuinely negligible anyway. React.memo() exists specifically to opt a particular component out of this default cascading behavior, telling React to skip re-rendering that component, and its own subtree, unless its props have genuinely changed, which is worth doing selectively for components that are either expensive to render or re-render very frequently due to an unrelated parent update.

Related Functions

renderingusememousecallback