Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to optimize the performance of React app ?
Next article icon

Performance Hooks in React

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

While developing React Applications, optimizing performance is one of the most important things for delivering a seamless user experience. One common way to boost performance is to minimize unnecessary re-renders by skipping repetitive calculations. React provides two powerful hooks, useMemo and useCallback, which helps you to achieve optimization by caching results and function definitions, respectively. In this article, we'll learn about the functionalities of these hooks and explore how they can be effectively utilized to enhance React application performance.

We will discuss about the following Performance Hooks in React:

Table of Content

  • useMemo Hook
  • useCallback Hooks

Both useMemo and useCallback are part of React's hooks API introduced in React 16.8. While they serve different purposes, they share a common goal of optimizing performance by memoizing values and functions.

useMemo Hook

In React useMemo Hook returns a memoized value and prevents the application from unnecessary re-renders. It is useful in heavy computations and processes when using functional components.

Syntax:

const memoizedValue = useMemo(functionThatReturnsValue, arrayDependencies)

Example: In this example, we have used the number as the squareNum will run only when the number changes. If we increase the counter and the number remains the same in the input field the squareNum doesn’t run again.

JavaScript
// Filename - App.js  import React, { useState, useMemo } from "react";  function App() {     const [number, setNumber] = useState(0);     // Using useMemo     const squaredNum = useMemo(() => {         return squareNum(number);     }, [number]);     const [counter, setCounter] = useState(0);      // Change the state to the input     const onChangeHandler = (e) => {         setNumber(e.target.value);     };      // Increases the counter by 1     const counterHander = () => {         setCounter(counter + 1);     };     return (         <div style={{marginLeft: "20%"}}>             <h1>Welcome to Geeksforgeeks</h1>             <input                 type="number"                 placeholder="Enter a number"                 value={number}                 onChange={onChangeHandler}>             </input>              <div>OUTPUT: {squaredNum}</div>             <button onClick={counterHander}>                 Counter ++             </button>             <div>Counter : {counter}</div>         </div>     ); }  // Function to square the value function squareNum(number) {     console.log("Squaring will be done!");     return Math.pow(number, 2); }  export default App; 

Output:

Animation34

useCallback Hooks

The useCallback hook is used when you have a component in which the child is rerendering again and again without need.

Pass an inline callback and an array of dependencies. useCallback will return a memoized version of the callback that only changes if one of the dependencies has changed. This is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders.

Syntax:

const memoizedCallback = useCallback(
() => {
doSomething(a, b);
},
[a, b],
);

Example: When we change the state ‘count’ then two functions will re-instantiated so the set size will increase by 2 and when we update the state ‘number’ then only one function will re-instantiated and the size of the set will increase by only one.

JavaScript
import React, { useState, useCallback } from 'react' var funccount = new Set(); const App = () => {      const [count, setCount] = useState(0)     const [number, setNumber] = useState(0)      const incrementCounter = useCallback(() => {         setCount(count + 1)     }, [count])     const decrementCounter = useCallback(() => {         setCount(count - 1)     }, [count])     const incrementNumber = useCallback(() => {         setNumber(number + 1)     }, [number])      funccount.add(incrementCounter);     funccount.add(decrementCounter);     funccount.add(incrementNumber);     alert(funccount.size);      return (         <div>             Count: {count}             <button onClick={incrementCounter}>                 Increase counter             </button>             <button onClick={decrementCounter}>                 Decrease Counter             </button>             <button onClick={incrementNumber}>                 increase number             </button>         </div>     ) }  export default App; 

Output:

Lightbox

Next Article
How to optimize the performance of React app ?

S

souravsharma098
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • React-Hooks

Similar Reads

  • New Hooks in React 18
    React's state, representing dynamic component data, is integral for effective component management. Initially confined to class components and managed using this.setState, the introduction of React Hooks in version 16.8 extended state usage to functional components. Hooks, functions enabling state a
    5 min read
  • Optimizing Performance in ReactJS
    Performance matters a lot in any application. In ReactJS, Optimizing the performance is an important task to do before launching a React application. There are different ways to explore optimizing a React application that can help to increase speed and efficiency as well as user experience.  ReactJS
    4 min read
  • What is Web Performance?
    Web performance encompasses how the website operates and the performance of the operations that are intended for the users. In today’s world filled with high-speed technologies, a slow website results in unsatisfied audiences, loss of traffic, and even some harm to your brand. It also leads to impro
    7 min read
  • How to optimize the performance of React app ?
    The operations involved in keeping the DOM updates are costly but react uses several techniques to minimize the no. of operations which leads to natively faster UI for many cases. The following techniques can be used to speed up the application: Table of Content Use binding functions in constructors
    3 min read
  • Resource Hooks in React
    In React, components often need to access external resources such as data from promises or context information for styling. Managing these resources within the component state could lead to unnecessary complexity and performance overhead. React provides a simple solution with the 'use' hook to overc
    3 min read
  • State Hooks in React
    State Hooks, introduced in React 16.8, revolutionized how developers manage state in functional components. Before State Hooks, state management was primarily confined to class components using the setState method. State Hooks, such as useState, enable functional components to manage local state eff
    3 min read
  • How memoization optimizes performance in React ?
    Have you ever wondered how computers can do complex tasks super fast? Well, memoization is like a secret trick that makes certain tasks much quicker by remembering past results instead of recalculating them every time. It's a game-changer for functions that do the same calculations over and over aga
    6 min read
  • Built-in React Hooks
    In React, built-in Hooks are like tools that empower your components with various functionalities. They give you the flexibility to use different features without having to create class components. You can either utilize the hooks provided by React or mix and match them to create custom hooks tailor
    5 min read
  • How to Optimize WebGL Performance?
    WebGL (Web Graphics Library) is a powerful JavaScript API used to render 3D and 2D graphics within any compatible web browser. However, achieving optimal performance with WebGL requires careful attention to various aspects of your code and rendering pipeline. Key strategies to optimize WebGL perform
    3 min read
  • Optimizing Performance with useMemo and useCallback Hooks
    In React applications, optimizing performance is crucial for ensuring smooth user experiences, especially in components with complex computations or frequent re-renders. Two hooks provided by React, useMemo, and useCallback, offer efficient ways to achieve performance improvements by memoizing value
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences