如何防止在React中使用纯子级重新渲染纯组件?

问题描述

我有一个功能性的React组件,在其中我使用id来防止不必要的重新渲染。如果它仅包含一个字符串作为子代,则仅在更改该字符串(如预期)时才呈现它。但是,当我添加一个也使用collections: - name: "about" # Used in routes,e.g.,/admin/collections/blog label: "About / Forum" # Used in the UI folder: "content/about/forum" # The path to the folder where the documents are stored create: true # Allow users to create new documents in this collection slug: "{{year}}-{{month}}-{{day}}-{{slug}}" # Filename template,YYYY-MM-DD-title.md fields: # The fields for each document,usually in front matter - {label: "Layout",name: "layout",widget: "hidden",default: "blog"} - {label: "Title",name: "title",widget: "string"})``` 的子组件时,总是将其重新渲染(即使未重新渲染子组件)。这是指示这些情况的示例代码

React.memo

第二种情况:

React.memo

更清楚地说,在第二种情况下,总是重新渲染父级(好像未使用const Parent = React.memo(() => <div> Parent </div>) // Rendered only once! 一样)。显然,不需要重新渲染它,因为它的子对象不会重新渲染,因此,父母的孩子都不会被更改。我有什么想念的吗?如何更改实现以避免重新呈现const Child = React.memo(() => <div> Child </div>); // Rendered only once! const Parent = React.memo(() => <Child />); // Rerendered everytime 组件?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)