ipanel-rnapp-ajb-base 1.0.37 → 1.0.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/hooks/useDynamicPackages.js +21 -11
- package/package.json +2 -2
|
@@ -1,22 +1,32 @@
|
|
|
1
|
-
import React, {useState,
|
|
1
|
+
import React, { useState, useEffect } from "react";
|
|
2
2
|
|
|
3
3
|
const useDynamicPackages = () => {
|
|
4
4
|
const [DynamicStackScreens, setDynamicScreens] = useState({});
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
const addDynamicScreens = screens => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const addDynamicScreens = (screens) => {
|
|
8
|
+
// 使用函数式更新,可以确保我们总是基于最新的状态进行更新
|
|
9
|
+
setDynamicScreens((currentScreens) => {
|
|
10
|
+
const allScreens = {
|
|
11
|
+
...currentScreens,
|
|
12
|
+
...screens,
|
|
13
|
+
};
|
|
14
|
+
return allScreens;
|
|
11
15
|
});
|
|
12
16
|
};
|
|
13
|
-
global.base.eventBus.removeAllListeners('addDynamicScreens');
|
|
14
|
-
global.base.eventBus.addListener('addDynamicScreens', addDynamicScreens);
|
|
15
|
-
}, [DynamicStackScreens]);
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
global.base.eventBus.addListener("addDynamicScreens", addDynamicScreens);
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
// useEffect 的清理函数:在组件卸载时移除监听,防止内存泄漏
|
|
21
|
+
return () => {
|
|
22
|
+
global.base.eventBus.removeListener(
|
|
23
|
+
"addDynamicScreens",
|
|
24
|
+
addDynamicScreens
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
}, []); // 空依赖数组意味着这个 effect 只在组件挂载时运行一次
|
|
28
|
+
|
|
29
|
+
return { DynamicStackScreens };
|
|
20
30
|
};
|
|
21
31
|
|
|
22
32
|
export default useDynamicPackages;
|