From the course: Master React 19 and Next.js 16 with Hands-On Projects and Real-World Applications

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

useMemo — memoizing expensive calculations

useMemo — memoizing expensive calculations

In this session, you will learn how to use the Use Memo hook in React to optimize performance by memoizing the results of expensive calculations. This prevents unnecessary recalculations, making your application run more smoothly, especially when dealing with complex operations. Let's start with a simple React component. Let's import the UseState hook from React. We'll use this hook to manage state within our component. Here I am adding two state variables to our component. Count is initialized to 0 and will be used later. Input is initialized to 5 and will be the input for our expensive calculation. Now, you will see me structuring the JSX to set up a demonstration of an expensive calculation. I add a div with some styling, a title Expensive Calculation Demo, and display the current input value. Let's define a function named ExpensiveCalculation that takes a number, num, as an argument. Let's add a console.log inside the ExpensiveCalculation function to indicate when it's running…

Contents