[英]Prevent Re-Render With Modifications to Parent State
如何在父组件中修改 state 并将其作为道具传递给子组件,但仅在修改后的 state 更改时重新渲染这些子组件?
我的基本设置目前有效,但它会导致一些不必要的重新渲染。 父组件从 URL(使用 react-router 挂钩)获取信息并对其进行修改,然后将其作为道具传递给其子组件。 该组件如下所示:
const myComponent = () => {
const { dataType } = useParams();
const { search } = useLocation();
/// These functions help me transform that data into something more manageable
const y = queryString.parse(search).collection;
const { collections, gqlQuery } = getCollections(dataType);
const collection = collections.find(x => x.value === y);
return (
<MyChild collection={collection} /> ... This component is re-rendering unnecessarily.
)
};
如何确保当 URL 不更改(使用 react-router 提取的dataType
和search
值)时,接收派生数据的子组件也不会不必要地重新渲染?
第一步是确保仅当其中一个依赖项发生更改时,对collection
变量的引用才会更改:
useMemo(() => {
const y = queryString.parse(search).collection;
const { collections, gqlQuery } = getCollections(dataType);
const collection = collections.find(x => x.value === y);
}, [search, dataType]);
第二个是确保组件仅在收到新道具时重新渲染:
import { memo } from 'react';
function Child() {
}
export default memo(Child);
您还可以使用memo
的第二个参数来自定义比较的内容。
function Child(props) {
}
function areEqual(prevProps, nextProps) {
/*
return true if passing nextProps to render would return
the same result as passing prevProps to render,
otherwise return false
*/
}
export default React.memo(Child, areEqual);
React.memo - 文档React.useMemo - 文档
PS:请注意,React 通常在重新渲染时非常快。 仅在衡量其影响后将其用作性能增强。 有可能它的性能比以前更差
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.