OnlineCBT Logo OnlineCBT
React Tutorial
REACT · LESSON 15

Performance Optimization: React.memo, useMemo, useCallback

By Ratan Updated: 16 Oct 2026

Prevent unnecessary component re-renders using React.memo, memoized values with useMemo, and useCallback.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

1. Why Optimize React Renders?

By default, when a parent component re-renders, React recursively re-renders all of its child components regardless of whether their props have changed. While React's Virtual DOM diffing is fast, unnecessary re-renders in large lists or complex data tables can cause perceptible UI lag.

2. React.memo: Memoizing Child Components

React.memo is a Higher-Order Component that skips re-rendering a component if its props have not changed (shallow comparison):

import React from 'react';

const QuestionOption = React.memo(function QuestionOption({ text, isSelected, onSelect }) {
  console.log("Rendered option:", text);
  return (
    
  );
});

export default QuestionOption;

3. useCallback: Memoizing Function References

In JavaScript, functions are objects. Every time a parent component re-renders, it creates a new function instance. Passing that new function down as a prop breaks React.memo! useCallback caches the function instance:

import { useState, useCallback } from 'react';

function QuestionScreen() {
  const [selectedId, setSelectedId] = useState(null);

  // useCallback caches the callback function across re-renders
  const handleSelect = useCallback((id) => {
    setSelectedId(id);
  }, []);

  return  handleSelect(1)} />;
}

4. useMemo: Caching Expensive Computations

Use useMemo to cache the return value of an expensive calculation so it only recalculates when dependencies change:

import { useMemo } from 'react';

function AnalyticsLeaderboard({ students }) {
  // Only re-calculates average percentile when students array changes
  const averageScore = useMemo(() => {
    console.log("Computing complex statistics...");
    if (!students.length) return 0;
    const total = students.reduce((acc, s) => acc + s.score, 0);
    return (total / students.length).toFixed(2);
  }, [students]);

  return 
Batch Average Score: {averageScore}%
; }

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat