Both have a similar name and are optimization methods. But that's where the similarity ends, they're totally distinct React features.
React.memo is for when you find an existing component that is expensive to render, and you don't have the option to optimize it internally. Just wrap it and let React do an extra check.
React.useMemo is for internally optimizing a component by saving the return value of an expensive function call.
Implementation
useMemo
The source code is relatively straightforward. The previous value is returned if the deps didn't change.
const prevDeps: Array<mixed> | null = prevState[1];
if (prevState !== null) {
if (areHookInputsEqual(nextDeps, prevDeps)) {
return prevState[0];
}
}
const nextValue = nextCreate();
hook.memoizedState = [nextValue, nextDeps];
return nextValue;
In all other cases it calls the (supposedly expensive) nextCreate function. The result is first saved in the hook log (for use in next renders) and then returned.
For the initial ("mount") render, React uses a simpler implementation that just calls nextCreate, saves it, and returns it.
React.memo
React.memo is quite a different type of function. It's a sort of Higher Order Component, but with a catch. It doesn't return a regular React component, but a special REACT_MEMO_TYPE. This type ultimately results in special handling of these elements while executing render work.
There's more moving parts to this but for the sake of simplicity I just copied the props check.
let compare = Component.compare; // This is what you pass into 2nd argument.
compare = compare !== null ? compare : shallowEqual;
if (compare(prevProps, nextProps) && current.ref === workInProgress.ref) {
return bailoutOnAlreadyFinishedWork(current, workInProgress, renderLanes);
}
Unlike useMemo it doesn't involve storing any additional state. Instead the comparison function is used to avoid calling the (supposedly expensive) render function. It simply aborts a chunk of work before starting it, when it sees it would be useless.
Conclusion
These are really 2 distinct things with a similar name and purpose.
Applicability to OP use case
You'll probably want React.memo.
Of course you always first measure if there's anything to optimize at all. Each of these things has a cost on its own. You can't sprinkle it around and automatically gain performance. If it's not balanced by avoiding expensive tasks, it's only making the code slower and more complex.
The following doesn't do anything useful:
useMemo(({ someProp }) => <someComponentHierarchy>...</someComponentHierarchy>)
If you check the React source above, you see the nextCreate function is called without arguments.
Regardless of what you put in it, useMemo will not result in the same check for the Memoized element type as React.memo does, so it can't be used for the same purpose.