简体   繁体   English

记忆组件的子组件是否也被记忆?

[英]Are child components of a memoized component memoized too?

Does the child component returned from useMemo or wrapped in React.memo() are memoized too?从 useMemo 返回或包装在 React.memo() 中的子组件是否也被记忆? (Apologies, this might be a silly question) (抱歉,这可能是一个愚蠢的问题)

For example:例如:

React.memo(<Parent><Child/></Parent>);

Here the Parent is memoized, is the Child too memoized?这里的 Parent 被记住了,Child 是不是太记住了?

The child isn't memoized in the sense that React doesn't apply React.memo to the child too.从 React 也没有将React.memo应用到孩子的意义上,孩子没有被记忆。

But the child is memoized in the following sense: Let's say component A is wrapped in React.memo and its child component B not.但是子组件在以下意义上被记忆:假设组件 A 被包裹在React.memo中,而它的子组件 B 没有。 If A is not re rendered because of memoization then B will not re render too.如果 A 因为记忆而没有重新渲染,那么 B 也不会重新渲染。

Also from the docs:同样来自文档:

React.memo only checks for prop changes. React.memo 仅检查 prop 更改。 If your function component wrapped in React.memo has a useState or useContext Hook in its implementation, it will still rerender when state or context change.如果包装在 React.memo 中的 function 组件在其实现中具有 useState 或 useContext Hook,它仍会在 state 或上下文更改时重新呈现。

So if components state gets modified, then it will be re rendered, despite if it or parent is memoized.因此,如果组件 state 被修改,那么它将被重新渲染,尽管它或父级已被记忆。

Also if A is re rendered because something in its state changed, then its child B will re render too (assuming B is not memoized).此外,如果 A 被重新渲染,因为它的state中的某些内容发生了变化,那么它的子 B 也将重新渲染(假设 B 没有被记忆)。

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

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