Introduction
In the previous article, we have learned about the concept of the useCallback() hook and how to implement that in React and why it is required. In this article, we will see another hook named useMemo() hook and its implementation.
useMemo() hook
The useMemo() hook allows you to memoize functions so that the function is prevented from being called on every render. It just includes a function containing an array of inputs which helps useMemo() hook to only recompute the memorized value when any of the input arrays have been changed.
Unlike useEffect() hook, the useMemo() hook does not trigger every time you change one of its dependencies.
The memoized function will always check to see if the dependencies need to be changed since the last render. If so, it executes the function and returns the result. And if it returns false, which dictates that nothing is changed, so it will return the cached result from the last execution. So, in this way useMemo() hook helps to improve performance in React application.
Syntax
- const memoizedValue = useMemo(() => computefunction(a, b), [a, b]);
Where a,b can be values that need to be processed and a and b in square brackets are to specify when function need to be processed.
useMemo() hook memoize takes a function that needs to be memoized and an array of values that when changed, would invalidate the memoization.
Now let’s look at the demo and how it is used for performance optimization.
Create a component named RandomWords which will return words randomly on the click of a button,
- import React, { useState } from 'react'
- function RandomWords() {
- const [count, setCount] = useState(0)
- const [wordIndex, setWordIndex] = useState(0)
- const words = ['This', 'is', 'React', 'Application', 'for', 'Testing', 'By', 'Priyanka']
- const word = words[wordIndex]
- const computeLetterCount = (word) => {
- let i = 0;
- while (i < 200000000) i++
- console.log(i)
- return word.length;
- };
- const letterCount = (word) => { computeLetterCount(word) };
- return (
- <div>
- <div style={{ padding: '15px' }}>
- <h2>Compute number of letters (slow)</h2>
- <p>"{word}" has {letterCount(word)} letters</p>
- <button
- onClick={() => {
- const next = wordIndex + 1 === words.length ? 0 : wordIndex + 1;
- setWordIndex(next);
- }}
- >
- Next word
- </button>
- <h2>Increment a counter (fast)</h2>
- <p>Counter: {count}</p>
- <button onClick={() => setCount(count + 1)}>Increment</button>
- </div>
- </div>
- )
- }
- export default RandomWords

Join the conversation! Your thoughts help the community grow.