@hzab/data-model 0.2.0 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/hooks.js +14 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/data-model",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "data model",
5
5
  "main": "src",
6
6
  "scripts": {
package/src/hooks.js CHANGED
@@ -1,7 +1,18 @@
1
- import { useMemo } from "react";
1
+ import { useMemo, useRef } from "react";
2
+ import { merge } from "lodash";
2
3
 
3
4
  import DataModel from "./data-model";
4
5
 
5
- export const useDataModel = function (params, effects = []) {
6
- return useMemo(() => new DataModel(params), effects);
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);
7
18
  };