![]() |
VOOZH | about |
React.memo and useMemo help improve performance by avoiding unnecessary re-computations. They allow React to reuse previously calculated results when inputs have not changed.
React.memo() is a higher-order component (HOC) provided by React that memoizes functional components. It means that it caches the result of the component’s rendering based on its props, and re-renders only when the props have changed.
const MemoizedComponent = React.memo(FunctionalComponent);useMemo() is a React Hook used to memoize the result of expensive computations within functional components. It memorizes the value returned by a provided function and re-calculates it only when the dependencies change.
const memoizedValue = useMemo(() => computeExpensiveValue(dep1, dep2), [dep1, dep2]);Step 1: Create a new React.js project and install the required dependencies:-
npx create-react-app my-react-app.Step 2: Navigate to the root directory of your project using the following command.
cd my-react-appExample: Below is an example of both React.memo() and useMemo().
Output: