Skip to content

Commit dfe87b3

Browse files
committed
Merge branch 'dev' of github.com:lowcoder-org/lowcoder into fix/event-handlers
2 parents a87e00b + b84dd48 commit dfe87b3

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

client/packages/lowcoder/src/comps/hooks/localStorageComp.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isEmpty } from "lodash";
33
import { simpleMultiComp, stateComp, withViewFn } from "../generators";
44
import { NameConfig, withExposingConfigs } from "../generators/withExposing";
55
import { JSONObject } from "../../util/jsonTypes";
6-
import { useEffect } from "react";
6+
import { useEffect, useMemo, useCallback } from "react";
77
import isEqual from "fast-deep-equal";
88
import { trans } from "i18n";
99
import log from "loglevel";
@@ -13,28 +13,36 @@ const APP_STORE_NAMESPACE = "lowcoder_app_local_storage";
1313
const LocalStorageCompBase = withViewFn(
1414
simpleMultiComp({ values: stateComp<JSONObject>({}) }),
1515
(comp) => {
16-
// add custom event listener to update values reactively
17-
useEffect(() => {
18-
const handler = () => {
19-
try {
20-
const raw = localStorage.getItem(APP_STORE_NAMESPACE) || "{}";
21-
const parsed = JSON.parse(raw);
22-
comp.children.values.dispatchChangeValueAction(parsed);
23-
} catch (e) {
24-
log.error("Failed to parse localStorage:", e);
25-
}
26-
};
16+
const originStore = localStorage.getItem(APP_STORE_NAMESPACE) || "{}";
17+
18+
const parseStore = useMemo(() => {
19+
try {
20+
return JSON.parse(originStore);
21+
} catch (e) {
22+
log.error("application local storage invalid");
23+
return {};
24+
}
25+
}, [originStore]);
26+
27+
const handleStorageUpdate = useCallback(() => {
28+
try {
29+
comp.children.values.dispatchChangeValueAction(parseStore);
30+
} catch (e) {
31+
log.error("Failed to parse localStorage:", e);
32+
}
33+
}, [parseStore, comp.children.values]);
2734

35+
useEffect(() => {
2836
// Add listener on mount
29-
window.addEventListener("lowcoder-localstorage-updated", handler);
37+
window.addEventListener("lowcoder-localstorage-updated", handleStorageUpdate);
3038

3139
// Run once on mount to initialize
32-
handler();
40+
handleStorageUpdate();
3341

3442
return () => {
35-
window.removeEventListener("lowcoder-localstorage-updated", handler);
43+
window.removeEventListener("lowcoder-localstorage-updated", handleStorageUpdate);
3644
};
37-
}, []);
45+
}, [handleStorageUpdate]);
3846

3947
return null;
4048
}

client/packages/lowcoder/src/pages/editor/editorView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,6 @@ function EditorView(props: EditorViewProps) {
498498

499499
return () => {
500500
window.removeEventListener(eventType, updateSize);
501-
savePanelStatus(panelStatus);
502-
saveEditorModeStatus(editorModeStatus);
503501
};
504502
}, [panelStatus, editorModeStatus]);
505503

@@ -553,6 +551,8 @@ function EditorView(props: EditorViewProps) {
553551
setShowShortcutList(false);
554552
setMenuKey(SiderKey.State);
555553
setHeight(undefined);
554+
savePanelStatus(panelStatus);
555+
saveEditorModeStatus(editorModeStatus);
556556
};
557557
}, []);
558558

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy