memo vs. useMemo
See original GitHub issueI’m a little confused about the relationship between memo()
(the core API function) and useMemo()
(the hook).
Specifically, can memo()
be implemented in terms of useMemo()
?
I tried this but it doesn’t work, it just keeps re-rendering:
const my_memo = Component => props => {
return React.useMemo(() => <Component {...props} />, [props]);
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
React.memo() vs. useMemo(): Major differences and use cases
React.memo() is a higher-order component that we can use to wrap components that we do not want to re-render unless props within them...
Read more >React.memo() vs. useMemo() - Bits and Pieces
useMemo () is one of the most used React Hooks among developers. It takes a function and a dependency array as input and...
Read more >React.memo vs useMemo - Atomized Objects
You can think of Memoization as a function that remembers something. In useMemo it remembers the value returned between renders, and in React.memo...
Read more >React.memo and useMemo - What's the Difference?
useMemo is a React hook that can be used to wrap a function or object, within a React component. Similarly to React.memo ,...
Read more >React.memo vs useMemo for private components
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...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
props
itself is a new object on every render.memo()
does shallow comparison by default. You can think ofmemo()
as auseMemo()
with a list of all your props like[props.foo, props.bar, props.foobar]
except you don’t need to type them all manually. It’s a shortcut for the most common case.This is about
shouldComponentUpdate
but the issue is similar: https://github.com/reactjs/reactjs.org/issues/1149. Basically — if your component breaks withoutmemo
then you’re not using it correctly. It only serves as perf optimization.Yeah — the problem with that is it’ll break if order or key changes or if you add or remove keys.