useMemo vs React.memo vs pureComponent

Viewed 1818

There are many ways to do one thing in react nowadays. Take memo for example, what's the difference between useMemo and memo(MyComponent) and React.PureComponent? Aren't they're doing the same thing? so which one we should use today? If we judge base on the date of the newer API we should use useMemo?

1 Answers

We can use useMemo and React.memo inside the functional component

  • React.memo: Used to memoize components.

Suppose you have two components, two of which are on the same level, and the other component is the parent of these two components. We pass our states from the parent component to the children. When a change is made to one of the child components, the other child component is re-rendered. React.memo is used to prevent this re-rendering.

Sorry about the painting

  • React.useMemo: Used to memoize value

When our calculations are allowed in an over function, this function is rendered with each re-rendering of the component. Using useMemo, we set the useMemo to be re-rendered only in certain cases

Related