Skip to content

Minimize rerendering #1147

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

Merged
merged 6 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added memoization
  • Loading branch information
raheeliftikhar5 committed Sep 2, 2024
commit 5b45716ef97da68cb31835bee75c42009d114276
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
DEFAULT_GRID_COLUMNS,
DEFAULT_ROW_HEIGHT,
} from "layout/calculateUtils";
import _ from "lodash";
import _, { isEqual } from "lodash";
import {
ActionExtraInfo,
changeChildAction,
Expand Down Expand Up @@ -313,7 +313,7 @@ const ItemWrapper = styled.div<{ $disableInteract?: boolean }>`
pointer-events: ${(props) => (props.$disableInteract ? "none" : "unset")};
`;

const GridItemWrapper = React.forwardRef(
const GridItemWrapper = React.memo(React.forwardRef(
(
props: React.PropsWithChildren<HTMLAttributes<HTMLDivElement>>,
ref: React.ForwardedRef<HTMLDivElement>
Expand All @@ -326,11 +326,11 @@ const GridItemWrapper = React.forwardRef(
</ItemWrapper>
);
}
);
));

type GirdItemViewRecord = Record<string, GridItem>;

export function InnerGrid(props: ViewPropsWithSelect) {
export const InnerGrid = React.memo((props: ViewPropsWithSelect) => {
const {
positionParams,
rowCount = Infinity,
Expand Down Expand Up @@ -385,7 +385,7 @@ export function InnerGrid(props: ViewPropsWithSelect) {

const canAddSelect = useMemo(
() => _.size(containerSelectNames) === _.size(editorState.selectedCompNames),
[containerSelectNames, editorState]
[containerSelectNames, editorState.selectedCompNames]
);

const dispatchPositionParamsTimerRef = useRef(0);
Expand Down Expand Up @@ -432,16 +432,21 @@ export function InnerGrid(props: ViewPropsWithSelect) {
onPositionParamsChange,
onRowCountChange,
positionParams,
props,
props.dispatch,
props.containerPadding,
]
);
const setSelectedNames = useCallback(
(names: Set<string>) => {
editorState.setSelectedCompNames(names);
},
[editorState]
[editorState.setSelectedCompNames]
);
const { width, ref } = useResizeDetector({ onResize, handleHeight: isRowCountLocked });

const { width, ref } = useResizeDetector({
onResize,
handleHeight: isRowCountLocked,
});

const itemViewRef = useRef<GirdItemViewRecord>({});
const itemViews = useMemo(() => {
Expand All @@ -464,9 +469,10 @@ export function InnerGrid(props: ViewPropsWithSelect) {
const clickItem = useCallback(
(
e: React.MouseEvent<HTMLDivElement,
globalThis.MouseEvent>, name: string
globalThis.MouseEvent>,
name: string,
) => selectItem(e, name, canAddSelect, containerSelectNames, setSelectedNames),
[canAddSelect, containerSelectNames, setSelectedNames]
[selectItem, canAddSelect, containerSelectNames, setSelectedNames]
);

useEffect(() => {
Expand Down Expand Up @@ -555,7 +561,9 @@ export function InnerGrid(props: ViewPropsWithSelect) {
{itemViews}
</ReactGridLayout>
);
}
}, (prevProps, newProps) => {
return isEqual(prevProps, newProps);
});

function selectItem(
e: MouseEvent<HTMLDivElement>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EditorContext } from "comps/editorState";
import { EditorContainer } from "pages/common/styledComponent";
import { Profiler, useContext, useRef, useState } from "react";
import React, { Profiler, useContext, useRef, useState } from "react";
import styled from "styled-components";
import { profilerCallback } from "util/cacheUtils";
import {
Expand All @@ -20,6 +20,7 @@ import { CanvasContainerID } from "constants/domLocators";
import { CNRootContainer } from "constants/styleSelectors";
import { ScrollBar } from "lowcoder-design";
import { defaultTheme } from "@lowcoder-ee/constants/themeConstants";
import { isEqual } from "lodash";

// min-height: 100vh;

Expand Down Expand Up @@ -72,7 +73,7 @@ function getDragSelectedNames(

const EmptySet = new Set<string>();

export function CanvasView(props: ContainerBaseProps) {
export const CanvasView = React.memo((props: ContainerBaseProps) => {
const editorState = useContext(EditorContext);
const [dragSelectedComps, setDragSelectedComp] = useState(EmptySet);
const scrollContainerRef = useRef(null);
Expand Down Expand Up @@ -166,4 +167,6 @@ export function CanvasView(props: ContainerBaseProps) {
</EditorContainer>
</CanvasContainer>
);
}
}, (prevProps, newProps) => {
return isEqual(prevProps, newProps);
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const InitialState = {
startPoint: undefined,
};

export class DragSelector extends React.Component<SectionProps, SectionState> {
class DragSelectorComp extends React.Component<SectionProps, SectionState> {
private readonly selectAreaRef: React.RefObject<HTMLDivElement>;

constructor(props: SectionProps) {
Expand Down Expand Up @@ -178,3 +178,5 @@ export class DragSelector extends React.Component<SectionProps, SectionState> {
};
}
}

export const DragSelector = React.memo(DragSelectorComp);
8 changes: 6 additions & 2 deletions client/packages/lowcoder/src/comps/comps/rootComp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
import RefTreeComp from "./refTreeComp";
import { ExternalEditorContext } from "util/context/ExternalEditorContext";
import { useUserViewMode } from "util/hooks";
import React from "react";
import { isEqual } from "lodash";

const EditorView = lazy(
() => import("pages/editor/editorView"),
Expand All @@ -55,7 +57,7 @@ const childrenMap = {
preload: PreloadComp,
};

function RootView(props: RootViewProps) {
const RootView = React.memo((props: RootViewProps) => {
const previewTheme = useContext(ThemeContext);
const { comp, isModuleRoot, ...divProps } = props;
const [editorState, setEditorState] = useState<EditorState>();
Expand Down Expand Up @@ -143,7 +145,9 @@ function RootView(props: RootViewProps) {
</PropertySectionContext.Provider>
</div>
);
}
}, (prevProps, nextProps) => {
return isEqual(prevProps, nextProps);
});

/**
* Root Comp
Expand Down
Loading