ReactJS

ReactJS | useMemo Hook with Example

Hello ReactJS friends,

In today’s blog, we will learn about useMemo Hook in ReactJS.

Previously we have discussed various types of hooks in ReactJS.

React useMemo Hook

The React useMemo Hook is used in the functional component and returns a memoized value. Memoization is used when you want to avoid recomputing the function for the next time for a given set of arguments. The memoization function remembers the result of a given set of inputs, so when the parameters are passed again, it returns the stored result without computing the function.

useMemo() is a built-in React hook that accepts two arguments — a function compute that computes a result and the dependencies array

Syntax of useMemo Hook in ReactJS:

const memoizedResult = useMemo(compute, dependencies);

const memoizedResult = useMemo(() =>
{
    return expensiveFunction(propA, propB);
}, [propA, propB]);

The React useMemo Hook returns a memoized value. The useMemo Hook only runs when one of its dependencies updates. That’s the nature of the useMemo() hook.

Example of useMemo Hook in ReactJS:

import { useState } from 'react';

export function CalculateFactorial()
{
    const [number, setNumber] = useState(1);
    const [inc, setInc] = useState(0);
    const factorial = factorialOf(number);
    const onChange = event =>
    {
        setNumber(Number(event.target.value));
    };
    const onClick = () => setInc(i => i + 1);
    return (
        <div>
            Factorial of
            <input type="number" value={number}
                onChange={onChange} />
            is {factorial}
            <button onClick={onClick}>Re-render</button>
        </div>
    );
}
function factorialOf(n)
{
    console.log('factorialOf(n) called!');
    return n <= 0 ? 1 : n * factorialOf(n - 1);
}

Conclusion:

This was all about the useMemo hook in ReactJS. If you have any doubts, kindly get in touch with me through the comment section. To learn more about ReactJS, stay connected with us.

Happy Coding!

Click to rate this post!
[Total: 9 Average: 3.4]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

6 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

1 day ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago