【发布时间】:2021-07-07 12:57:34
【问题描述】:
我正在使用 why-did-you-render 包来调试我的 react 应用程序中的一些性能问题。我收到以下控制台消息,说明我的 XDrag 组件为何重新渲染:
XDrag
defaultNotifier.js:40 {XDrag: ƒ} "Re-rendered because of props changes:"
defaultNotifier.js:45 props.children
defaultNotifier.js:49 different React elements (remember that the <jsx/> syntax always produces a *NEW* immutable React element so a component that receives <jsx/> as props always re-renders). (more info here)
defaultNotifier.js:52 {prev children: {…}} "!==" {next children: {…}}
在the article linked to in this message, 中,他们建议在我们的原始组件周围使用“纯包装器”组件来避免这种重新渲染:
class PureFatherWrapper extends React.PureComponent{ render(){ return ( <PureFather> <SomeChild/> </PureFather> ) } }
- 纯包装器/纯组件到底是什么意思?纯组件与其他种类的组件有何不同?
- 为什么这个纯包装器会避免重新渲染?
- 如何为我的 XDrag 组件实现 Pure Wrapper?
import React, { FC, ReactNode } from "react";
import { Draggable, DraggableProps } from "react-beautiful-dnd";
interface IXDrag extends Omit<DraggableProps, "children"> {
className?: string;
children: ReactNode;
dragAll?: boolean;
}
const XDrag: FC<IXDrag> = ({ className, children, dragAll, ...props }) => {
console.log(React.isValidElement(children));
if (!React.isValidElement(children)) return <div />;
return (
<Draggable {...props}>
{(provided, snapshot) => {
const dragHandleProps = dragAll ? provided.dragHandleProps : {};
return (
<tr
className={className}
ref={provided.innerRef}
{...provided.draggableProps}
{...dragHandleProps}
>
{React.cloneElement(children, { provided })}
</tr>
);
}}
</Draggable>
);
};
使用 XDrag:
import React from "react";
import { useStoreState } from "../../hooks";
import XDrag from "../XDrag";
import TableHeader from "./TableHeader";
const ContentTable = () => {
const cardItems = useStoreState(
(state) => state.appModel.activeCards
);
return (
<table>
<tbody>
<tr>
<TableHeader
title={"URL"}
/>
</tr>
{cardItems.map((card, i) => {
return (
<XDrag
draggableId={card.sourceId}
index={i}
key={i.toString()}
isDragDisabled={card.isActive}
>
<td>{card.src}</td>
</XDrag>
);
})}
</tbody>
</table>
);
};
一些上下文:
我正在使用react beautiful dnd 库在表格中创建一些可拖动的行。每个XDrag 为所有<td>s 提供一个<tr>。
【问题讨论】:
-
您能否也发布一下您是如何使用
<XDrag>的?我的意思是,呈现它的父组件。
标签: reactjs