Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Editor Rerender Performance #157

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/components/CustomNode/ObjectNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import * as Styled from "./styles";

const inViewport = true;

const ObjectNode: React.FC<CustomNodeProps<[string, string][]>> = ({
type ObjectNodeProps = CustomNodeProps<[string, string][]>;

const ObjectNode: React.FC<ObjectNodeProps> = ({
width,
height,
value,
Expand Down Expand Up @@ -46,4 +48,8 @@ const ObjectNode: React.FC<CustomNodeProps<[string, string][]>> = ({
);
};

export default ObjectNode;
function propsAreEqual(prev: ObjectNodeProps, next: ObjectNodeProps) {
return prev.width === next.width && prev.height === next.height;
}

export default React.memo(ObjectNode, propsAreEqual);
15 changes: 11 additions & 4 deletions src/components/CustomNode/TextNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ const StyledTextNodeWrapper = styled.div<{ hasCollapse: boolean }>`
width: 100%;
`;

const TextNode: React.FC<
CustomNodeProps<string> & { node: NodeData; hasCollapse: boolean }
> = ({
type TextNodeProps = CustomNodeProps<string> & {
node: NodeData;
hasCollapse: boolean;
};

const TextNode: React.FC<TextNodeProps> = ({
node,
width,
height,
Expand Down Expand Up @@ -99,4 +102,8 @@ const TextNode: React.FC<
);
};

export default TextNode;
function propsAreEqual(prev: TextNodeProps, next: TextNodeProps) {
return prev.value === next.value;
}

export default React.memo(TextNode, propsAreEqual);
10 changes: 2 additions & 8 deletions src/components/CustomNode/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { Label, Node, NodeProps } from "reaflow";
import { Node, NodeProps } from "reaflow";
import ObjectNode from "./ObjectNode";
import TextNode from "./TextNode";

Expand All @@ -12,17 +12,11 @@ export interface CustomNodeProps<T> {
y: number;
}

const baseLabelStyle = {
fill: "transparent",
stroke: "transparent",
strokeWidth: 0,
};

export const CustomNode = (nodeProps: NodeProps) => {
const { properties } = nodeProps;

return (
<Node {...nodeProps} label={<Label style={baseLabelStyle} />}>
<Node {...nodeProps} label={<React.Fragment />}>
{({ width, height, x, y, node }) => {
if (Array.isArray(properties.text)) {
return (
Expand Down