@t8/react-store 1.0.30 → 1.0.32
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/dist/index.js +2 -7
- package/package.json +2 -2
- package/src/useStore.ts +9 -15
package/dist/index.js
CHANGED
|
@@ -34,7 +34,6 @@ import { useEffect, useMemo, useRef, useState } from "react";
|
|
|
34
34
|
function useStore(store, shouldUpdate = true) {
|
|
35
35
|
if (!isStore(store)) throw new Error("'store' is not an instance of Store");
|
|
36
36
|
let [, setRevision] = useState(-1);
|
|
37
|
-
let initedRef = useRef(false);
|
|
38
37
|
let state = store.getState();
|
|
39
38
|
let setState = useMemo(() => store.setState.bind(store), [store]);
|
|
40
39
|
let initialStoreRevision = useRef(store.revision);
|
|
@@ -44,14 +43,10 @@ function useStore(store, shouldUpdate = true) {
|
|
|
44
43
|
if (typeof shouldUpdate !== "function" || shouldUpdate(nextState, prevState))
|
|
45
44
|
setRevision(Math.random());
|
|
46
45
|
});
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
if (store.revision !== initialStoreRevision.current)
|
|
50
|
-
setRevision(Math.random());
|
|
51
|
-
}
|
|
46
|
+
if (store.revision !== initialStoreRevision.current)
|
|
47
|
+
setRevision(Math.random());
|
|
52
48
|
return () => {
|
|
53
49
|
unsubscribe();
|
|
54
|
-
initedRef.current = false;
|
|
55
50
|
initialStoreRevision.current = store.revision;
|
|
56
51
|
};
|
|
57
52
|
}, [store, shouldUpdate]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/react-store",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.32",
|
|
4
4
|
"description": "Concise shared state management for React apps",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@playwright/test": "^1.56.0",
|
|
38
|
-
"@t8/serve": "^0.1.
|
|
38
|
+
"@t8/serve": "^0.1.33",
|
|
39
39
|
"@types/node": "^24.5.2",
|
|
40
40
|
"@types/react": "^19.1.10",
|
|
41
41
|
"@types/react-dom": "^19.1.9",
|
package/src/useStore.ts
CHANGED
|
@@ -13,23 +13,24 @@ export function useStore<T>(
|
|
|
13
13
|
*
|
|
14
14
|
* @defaultValue `true`
|
|
15
15
|
*
|
|
16
|
-
*
|
|
17
|
-
* store
|
|
18
|
-
*
|
|
16
|
+
* `shouldUpdate` can be set to `false` to prevent subscription
|
|
17
|
+
* to the store updates. Use case: when the component only requires
|
|
18
|
+
* the store state setter but not the store state value, the
|
|
19
|
+
* component may not need to respond to the store updates:
|
|
19
20
|
*
|
|
20
21
|
* ```ts
|
|
21
22
|
* let [, setValue] = useStore(store, false);
|
|
22
23
|
* ```
|
|
23
24
|
*
|
|
24
|
-
*
|
|
25
|
-
* make the component respond only to specific store state changes
|
|
25
|
+
* `shouldUpdate` can be set to a function `(nextState, prevState) => boolean`
|
|
26
|
+
* to make the component respond only to specific store state changes,
|
|
27
|
+
* when this function returns `true`.
|
|
26
28
|
*/
|
|
27
29
|
shouldUpdate: ShouldUpdate<T> = true,
|
|
28
30
|
): [T, SetStoreState<T>] {
|
|
29
31
|
if (!isStore(store)) throw new Error("'store' is not an instance of Store");
|
|
30
32
|
|
|
31
33
|
let [, setRevision] = useState(-1);
|
|
32
|
-
let initedRef = useRef(false);
|
|
33
34
|
|
|
34
35
|
let state = store.getState();
|
|
35
36
|
let setState = useMemo(() => store.setState.bind(store), [store]);
|
|
@@ -46,18 +47,11 @@ export function useStore<T>(
|
|
|
46
47
|
setRevision(Math.random());
|
|
47
48
|
});
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (!initedRef.current) {
|
|
52
|
-
initedRef.current = true;
|
|
53
|
-
|
|
54
|
-
if (store.revision !== initialStoreRevision.current)
|
|
55
|
-
setRevision(Math.random());
|
|
56
|
-
}
|
|
50
|
+
if (store.revision !== initialStoreRevision.current)
|
|
51
|
+
setRevision(Math.random());
|
|
57
52
|
|
|
58
53
|
return () => {
|
|
59
54
|
unsubscribe();
|
|
60
|
-
initedRef.current = false;
|
|
61
55
|
initialStoreRevision.current = store.revision;
|
|
62
56
|
};
|
|
63
57
|
}, [store, shouldUpdate]);
|