简体   繁体   English

useMemo 与 React.memo 与 pureComponent

[英]useMemo vs React.memo vs pureComponent

There are many ways to do one thing in react nowadays.现在有很多方法可以在 React 中做一件事。 Take memo for example, what's the difference between useMemo and memo(MyComponent) and React.PureComponent ?以 memo 为例, useMemomemo(MyComponent)React.PureComponent什么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 ?如果我们根据较新 API 的日期进行判断,我们应该使用useMemo吗?

We can use useMemo and React.memo inside the functional component我们可以在功能组件中使用useMemoReact.memo

  • React.memo: Used to memoize components. React.memo:用于记忆组件。

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 .当对其中一个子组件进行更改时,会re-rendered另一个子组件 React.memo is used to prevent this re-rendering. React.memo 用于防止这种重新渲染。

对不起这幅画

  • React.useMemo: Used to memoize value React.useMemo:用于记忆

When our calculations are allowed in an over function, this function is rendered with each re-rendering of the component.当我们的计算在 over 函数中被允许时,这个函数会随着组件的每次重新渲染渲染 Using useMemo , we set the useMemo to be re-rendered only in certain cases使用useMemo ,我们将useMemo设置为仅在某些情况下重新呈现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM