为什么React.memo不使用`props.Children'的财产?
我正在尝试在react.memo()
中将子组件(“ box”)包裹起来,以避免当父组件从状态更改中重新呈现时,避免了不必要的重新订阅者。但这与props.Children
属性不起作用。当我在不使用props.Children
的情况下再次尝试时,它确实有效!但为什么?如果不是Memo
,我该如何使用构图和props.Cops.Cops.Cops.Cops.Cops.Cops.Cops.Cops.Cops.copss.?我该如何实现?
(1)传递组件示例(工作):&lt; box title =“我是一个框” /&gt; < /code>
(2)传递组件示例(无效):&lt ; box&gt; &lt; h1&gt; i是一个盒子&lt;/h1&gt; &lt;/box&gt;
///
工作示例(1):
导出默认备忘录(function box(props){ 返回 ( &lt; div className ='box'&gt;&lt; h1&gt; {props.title}&lt;/h1&gt;&gt;&lt;/div&gt;
) });
不工作示例(2):
导出默认备忘录(功能框(props){ 返回 ( &lt; div className ='box'&gt; {props.children}&lt;/div&gt;
) });
I'm trying to wrap a subcomponent ("Box") in React.memo()
to avoid the unnecessary re-renders of it when the parent component re-renders from state change. But this didn't work with props.children
property. When I tried again without using props.children
, it did work! But why? And how can i achieve this using composition and props.children
property if not with memo
?
(1) Passing the component example (worked): <Box title="i am a box" />
(2) Passing the component example (didn't work): <Box> <h1>I am a box</h1> </Box>
///
Working example (1):
export default memo (function Box(props) {
return (<div className='box'><h1>{props.title}</h1></div>
)
} );
Not working example (2):
export default memo (function Box(props) {
return (<div className='box'>{props.children}</div>
)
} );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以解释一下。
基于上述内容,为什么这在您的示例上不起作用。您小时候正在传递“ H1”元素。在您的子部分中,有一个“ H1”元素的数组。对于父母的每个渲染,将会出现一个新的阵列。是的,它将是一个具有相同内容的数组,但不是相同的数组,而不是相同的引用。备忘录没有进行深入检查。
下面有两个示例,带有字符串和元素。
但是,如果您不想放弃元素并想记住它们怎么办?
我建议使用Usememo Hook回忆过世的孩子。
我不会说那很好。但是,如果您的目标是记住孩子,这将有所帮助。您无法用备忘录解决这个问题。没有深入的比较,因此备忘录和孩子存在问题。不要忘记在usememo中指定依赖项,示例中没有一个,因此依赖项数组为空。
I can explain this.
Based on the above, why doesn't this work on your example. You are passing the "h1" element as children. In your child component comes an array with one "h1" element. And for each render of the parent, a new array will come. Yes, it will be an array with the same content, but it is not the same array, not the same reference. Memo does not do deep checks.
Below two examples, with string and elements.
But what if you don’t want to abandon the elements and want to memoize them?
I suggest use useMemo hook for memoize passed children.
I won't say that's good. But it will help if your goal is to memoize children. You can't solve this problem with memo. There is no deep comparison, so there is a problem with memo and children. Don't forget to specify dependencies in useMemo, there are none in the example, so the dependencies array is empty.
该组件的孩子实际上来自父母。当父级重新租赁时,它会重新计算所有内容,并触发返回语句。该返回声明中的所有内容均已重新创建,并要求所有孩子重新渲染。
其中一些可能是记忆的组件,这将决定是否应根据道具重新渲染。但是,当您将孩子转到该组件时,对于父母的每一个重新渲染,您都会将新的孩子的新实例发送到该回忆的组成部分。因此,它使其重新渲染。
Children of the component are actually coming from its parent. When parent re-renders, it recalculates everything and the return statement is triggered. Everything in that return statement is re-created and all the children are asked to re-render.
Some of them might be memoized components, which will decide if they should re-render based on the props. However, when you pass children to that component, for every re-render of the parent, you send a new instance of children to that memoized component. Hence, it causes it to re-render.