question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I’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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
gaearoncommented, Jan 17, 2019

I tried this but it doesn’t work, it just keeps re-rendering:

props itself is a new object on every render. memo() does shallow comparison by default. You can think of memo() as a useMemo() 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.

Can someone please clarify what sort of bugs and how to avoid them? Thanks!

This is about shouldComponentUpdate but the issue is similar: https://github.com/reactjs/reactjs.org/issues/1149. Basically — if your component breaks without memo then you’re not using it correctly. It only serves as perf optimization.

4reactions
gaearoncommented, Jan 17, 2019

Yeah — the problem with that is it’ll break if order or key changes or if you add or remove keys.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found