@hzab/data-model 0.1.2 → 0.2.1
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/README.md +10 -1
- package/package.json +1 -1
- package/src/hooks.js +18 -0
- package/src/index.js +1 -0
package/README.md
CHANGED
|
@@ -9,7 +9,16 @@
|
|
|
9
9
|
## 示例
|
|
10
10
|
|
|
11
11
|
```jsx
|
|
12
|
-
import DataModel
|
|
12
|
+
import DataModel, {
|
|
13
|
+
// 设置 token 到 localStorage 及 cookie,可以设置 opt.hasCookie(setToken(token, { hasCookie: false })) 关闭设置 token 到 cookie 上
|
|
14
|
+
setToken,
|
|
15
|
+
// 从 cookie 或 localStorage 获取 token
|
|
16
|
+
getToken,
|
|
17
|
+
// 设置默认兜底的错误信息
|
|
18
|
+
setDefaultErrMsg,
|
|
19
|
+
// 设置 DataModel 中默认的 axios
|
|
20
|
+
setDefaultAxios,
|
|
21
|
+
} from "@hzab/data-model";
|
|
13
22
|
|
|
14
23
|
const listDM = new DataModel({
|
|
15
24
|
// query 参数
|
package/package.json
CHANGED
package/src/hooks.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useMemo, useRef } from "react";
|
|
2
|
+
import { merge } from "lodash";
|
|
3
|
+
|
|
4
|
+
import DataModel from "./data-model";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 解决 hooks 重复实例化导致 query 丢失的问题
|
|
8
|
+
* @param {*} params
|
|
9
|
+
* @param {*} effects
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export const useDataModel = function (params = {}, effects = []) {
|
|
13
|
+
const model = useRef(new DataModel(params));
|
|
14
|
+
return useMemo(() => {
|
|
15
|
+
merge(model.current, params);
|
|
16
|
+
return model.current;
|
|
17
|
+
}, effects);
|
|
18
|
+
};
|