Skip to content

Commit

Permalink
memoize components
Browse files Browse the repository at this point in the history
  • Loading branch information
MutazAshhab committed Oct 15, 2023
1 parent 3dc3773 commit 9db8a2a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/components/Canvas/Decorations/Building.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import { Group, Rect, Text } from 'react-konva';

import { Decoration } from '~/zustand/useDecorations';
Expand All @@ -6,7 +7,7 @@ interface BuildingProps {
building: Decoration;
}

export function Building(props: BuildingProps) {
function BuildingComponent(props: BuildingProps) {
const { x, y } = props.building;

// Squares!
Expand Down Expand Up @@ -41,3 +42,5 @@ export function Building(props: BuildingProps) {
</Group>
);
}

export const Building = React.memo(BuildingComponent);
5 changes: 4 additions & 1 deletion src/components/Canvas/Decorations/Tree.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import { Circle, Group } from 'react-konva';

import seedrandom from 'seedrandom';
Expand All @@ -19,7 +20,7 @@ interface TreeProps {
tree: Decoration;
}

export function Tree(props: TreeProps) {
function TreeComponent(props: TreeProps) {
const rng = seedrandom(props.tree.seed);

const leaves = Array.from({ length: 50 }).map(() => ({
Expand All @@ -41,3 +42,5 @@ export function Tree(props: TreeProps) {
</Group>
);
}

export const Tree = React.memo(TreeComponent);
5 changes: 4 additions & 1 deletion src/components/Canvas/Intersection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useMemo, useState } from 'react';
import React from 'react';
import { Circle, Group } from 'react-konva';

import { KonvaEventObject } from 'konva/lib/Node';
Expand All @@ -19,7 +20,7 @@ interface IntersectionProps {
node: Node;
}

export function Intersection({ node }: IntersectionProps) {
function IntersectionComponent({ node }: IntersectionProps) {
const network = useNetworkStore();
const selector = useSelector();
const toolbarState = useToolbarStore();
Expand Down Expand Up @@ -127,3 +128,5 @@ export function Intersection({ node }: IntersectionProps) {
</Group>
);
}

export const Intersection = React.memo(IntersectionComponent);
5 changes: 4 additions & 1 deletion src/components/Canvas/Road.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import { useState } from 'react';
import { Arrow, Group } from 'react-konva';

Expand All @@ -16,7 +17,7 @@ interface RoadProps {
offset?: number;
}

export function Road({ edge, offset = 0 }: RoadProps) {
function RoadComponent({ edge, offset = 0 }: RoadProps) {
const network = useNetworkStore();
const selector = useSelector();

Expand Down Expand Up @@ -146,3 +147,5 @@ export function Road({ edge, offset = 0 }: RoadProps) {
</Group>
);
}

export const Road = React.memo(RoadComponent);

0 comments on commit 9db8a2a

Please sign in to comment.