While memo
is a HOC and useMemo
is a hook, you can use them the achieve the same result.
For context, HOC is an older React pattern that has been used for many years with class-based and functional components alike. You can still use it today (there's no plan for deprecation).
Hooks is a relatively new concept (about a year) that enhances functional components and in many cases drastically simplifies code. That's why many developers are moving towards using hooks.
Anyway, both memo
and useMemo
take two arguments: a function and props. If none of the props change on subsequent re-renders, the function is not executed again and instead returns the previous result. This, in effect, replaces shouldComponentUpdate
callbacks, with a purely functional approach.
With memo
, your code would look like this:
const SpeakerCardDetail = React.memo(
(props) => <div>{props.name}</div>
)
With useMemo
, you'd write:
const SpeakerCardDetail = (props) => useMemo(() => <div>{props.name}</div>)
Notice that useMemo
is used inside of your component function, while memo
wraps the function.
More traditionally, useMemo
could be written as:
function SpeakerCardDetail(props) {
return useMemo(
() => <div>{props.name}</div>
)
}
Now, the code above would re-render every time, making the useMemo
function a bit useless. To make it work its magic, we need to add the second argument. (memo
still works even without specifying the second argument but you can add it to customize it)
There's a slight difference in the format of the second argument. memo
expects a function that compares previous and current props, just like shouldComponentUpdate
does for class components.
const SpeakerCardDetail = React.memo(
(props) => <div>{props.name}</div>
,
// return true if passing nextProps to render would return the same result as passing prevProps to render, otherwise return false
(prevProps, nextProps) => prevProps.name === nextProps.name
)
useMemo
, on the other hand, expects an array as the second argument. Whenever the values in the array change, the function would be executed again.
function SpeakerCardDetail(props) {
return useMemo(
() => <div>{props.name}</div>
,
[props.name]
)
}
There's really no more magic than that. Both memo
and useMemo
are used to memoize the result of a function, the only difference is memo
is a HOC (and can be used to wrap both class and functional components) which useMemo
is a hook (and can only be used inside functional components).
useMemo
? – TwinberryReact.memo
allows functional component to have same optimization asPureComponent
provides for class-based components.useMemo
is for memoizing function's calls inside functional component. It would be hard to replace one with another since they have different purposes. – Kathrinekathryn