3

Both React.memo and the useMemo hook allow performance optimizations by reducing recalculation and rerendering. However, they work definitely in that React.memo is used to wrap a functional component and the useMemo hook can be used on almost any function, even pure calculations. More importantly, useMemo is typically called from a parent to a child component, while React.memo is typically called on the declaration of the child itself.

While both have different advantages, one advantage of React.memo is that it doesn't have to be called from each parent to child relationship. However, with the release of hooks, it seems apparent that React development wants to move towards functional components that use hooks to deal with state, side-effect events, and other effects. While I don't think that the React development team would have the courage or disregard of their user base to remove React.memo any time in the next 2 years, it makes me wonder if they want end users to stop using React.memo for stylistic reasons. Just as they have kind of passive aggressively moved away from class components in favor of functional components with hooks.

When using functional components with hooks, is the react framework moving away from React.memo? Would it be better to get used to using the hook version if one wants to keep up with React best practices in the future?

Mark Rogers
  • 96,497
  • 18
  • 85
  • 138

1 Answers1

12

The short answer is no.

Both are used to optimise performance in regard to reducing unnecessary re-rendering, but React.memo and useMemo are used for two different scenarios...

React.memo is a HOOC and informs react to perform a shallow comparison of the passed in props to determine whether to re-render.

https://reactjs.org/docs/react-api.html#reactmemo

Example:

export const Component = React.memo(({name}) => `Hello ${name}`)

Here, react will to a shallow comparison, and will only re-render if name has changed.

useMemo is a hook and is used to memoize a value. React will only only re-evaluate the value if the dependencies (second arg of useMemo) change. There are usage rules with hooks that should be followed.

https://reactjs.org/docs/hooks-reference.html#usememo

Example:

export const MyComponent = ({firstName, lastName, age}) => {

    const fullName = useMemo(() => `${firstName} ${lastName}`, [firstName, lastName]);

    return <Profile fullName={fullName} />

}

You could hack useMemo to do something like React.memo, but it is not intended to be used that way.

useCallback There is also the useCallback hook that is often used with React.memo.

If your parent component passes a callback to a child that is wrapped in React.memo , it's a good idea to create the callback using useCallback otherwise the child will re-render anyway due to the callback being re-created each time the parent re-renders.

useCallback also takes a dependency array like useMemo so that it can be re-created if a dependency changes.

https://reactjs.org/docs/hooks-reference.html#usecallback

Example:

export const MyComponent = ({firstName, lastName, age}) => {

    const handleClick = useCallback((e) => {
        e.preventDefault();
        // doSomething
    }, []);

    return <Profile onClick={handleClick} />

}

const Profile = React.memo((onClick) => (
    <button onClick={onClick)>Click me!</button>
));
Mark Rogers
  • 96,497
  • 18
  • 85
  • 138
Tom Brown
  • 421
  • 3
  • 8
  • I understand what both are and how they work and how they don't exactly overlap in usage. And I also understand that they have no public plans to deprecate React.memo. But what I'm trying to get at is whether stylistic best practice is moving away from React.memo. Just as with the advent of hooks, some users and teams began moving away from class components to keep up with the style signals the react development team is sending. – Mark Rogers Apr 18 '20 at 18:33
  • It's a good question. Personally, I can't think of a practical way the api could change to achieve the same effect. To answer you specifically, I agree that users could be less inclined to use React.memo for stylistic reasons, and I can also see react itself taking more responsibility on the decision to re-render / update. – Tom Brown Apr 18 '20 at 18:55
  • 1
    Per Tom's answer, the question doesn't really make sense conceptually. `React.memo()` and `useMemo()` do different things, for different use cases. There's no "moving away from React.memo()" going on. It's like asking "will hammers replace spatulas?". – markerikson Apr 18 '20 at 19:19
  • I agree they have different use cases and work in ways that are very different, but this is a case where the spatula has a blunt end so could be made to work like a hammer. While it might result in some ugly unencapsulated code, I could probably replace many usages of `React.memo`. With the advent of hooks, it seems like the devs were saying this is React 3.0 and these are the new ways we would like you to write things. It feels like React goes through cycles of jettisoning previous concepts. I'm wondering if they've hinted whether memo is going to be part of one of those purges. – Mark Rogers Apr 18 '20 at 20:41