@t8/react-store 1.0.19 → 1.0.21
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 +30 -10
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
Installation: `npm i @t8/react-store`
|
|
13
13
|
|
|
14
|
-
##
|
|
14
|
+
## Shared state setup
|
|
15
15
|
|
|
16
16
|
Moving the local state to the full-fledged shared state:
|
|
17
17
|
|
|
@@ -51,9 +51,11 @@ Moving the local state to the full-fledged shared state:
|
|
|
51
51
|
|
|
52
52
|
🔹 The shared state setup with `@t8/react-store` is very similar to `useState()` allowing for quick migration from local state to shared state or the other way around.
|
|
53
53
|
|
|
54
|
-
🔹 The `false` parameter in `useStore(store, false)` (as in `<ResetButton>` above) tells the hook not to subscribe the component to tracking the store state updates. The common use case is when
|
|
54
|
+
🔹 The `false` parameter in `useStore(store, false)` (as in `<ResetButton>` above) tells the hook not to subscribe the component to tracking the store state updates. The common use case is when a component makes use of the store state setter without using the store state value.
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
## Single store or multiple stores
|
|
57
|
+
|
|
58
|
+
An application can have as many stores as needed, whether on a single React Context or multiple Contexts.
|
|
57
59
|
|
|
58
60
|
```js
|
|
59
61
|
let AppContext = createContext({
|
|
@@ -62,7 +64,17 @@ let AppContext = createContext({
|
|
|
62
64
|
});
|
|
63
65
|
```
|
|
64
66
|
|
|
65
|
-
🔹
|
|
67
|
+
🔹 Splitting data into multiple stores helps maintain more targeted subscriptions to data changes in components.
|
|
68
|
+
|
|
69
|
+
## Filtering store updates
|
|
70
|
+
|
|
71
|
+
When only the store state setter is required, without the store state value, we can opt out from subscription to store state changes by passing `false` as the parameter of `useStore()`:
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
let [, setState] = useState(store, false);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Apart from a boolean, `useStore(store, shouldUpdate)` can take a function of `(nextState, prevState) => boolean` as the second parameter to filter store updates to respond to:
|
|
66
78
|
|
|
67
79
|
```jsx
|
|
68
80
|
let ItemCard = ({ id }) => {
|
|
@@ -83,7 +95,9 @@ let ItemCard = ({ id }) => {
|
|
|
83
95
|
};
|
|
84
96
|
```
|
|
85
97
|
|
|
86
|
-
|
|
98
|
+
## Providing shared state
|
|
99
|
+
|
|
100
|
+
Shared state can be provided to the app by means of a regular React Context provider:
|
|
87
101
|
|
|
88
102
|
```diff
|
|
89
103
|
let App = () => (
|
|
@@ -94,18 +108,24 @@ let ItemCard = ({ id }) => {
|
|
|
94
108
|
);
|
|
95
109
|
```
|
|
96
110
|
|
|
97
|
-
|
|
111
|
+
## Store data
|
|
112
|
+
|
|
113
|
+
A store can contain data of any type.
|
|
98
114
|
|
|
99
115
|
Live demos:<br>
|
|
100
116
|
[Primitive value state](https://codesandbox.io/p/sandbox/rtng37?file=%2Fsrc%2FPlusButton.jsx)<br>
|
|
101
117
|
[Object value state](https://codesandbox.io/p/sandbox/y7wt2j?file=%2Fsrc%2FPlusButton.jsx)
|
|
102
118
|
|
|
103
|
-
|
|
119
|
+
## With Immer
|
|
120
|
+
|
|
121
|
+
Immer can be used with `useStore()` just the same way as [with `useState()`](https://immerjs.github.io/immer/example-setstate#usestate--immer) to facilitate deeply nested data changes.
|
|
104
122
|
|
|
105
123
|
[Live demo with Immer](https://codesandbox.io/p/sandbox/rn4qsr?file=%2Fsrc%2FPlusButton.jsx)
|
|
106
124
|
|
|
107
|
-
|
|
125
|
+
## Shared loading state
|
|
126
|
+
|
|
127
|
+
The ready-to-use hook from the [T8 React Pending](https://github.com/t8js/react-pending) package helps manage shared async action state without disturbing the app's state management and actions' code.
|
|
108
128
|
|
|
109
|
-
|
|
129
|
+
## Standalone store
|
|
110
130
|
|
|
111
|
-
|
|
131
|
+
A store initialized outside a component can be used as the component's remount-persistent state.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/react-store",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.21",
|
|
4
4
|
"description": "Straightforward and minimalist shared state management for React apps",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"clean": "node -e \"require('node:fs').rmSync('dist', {force: true, recursive: true});\"",
|
|
10
10
|
"compile": "npx esbuild index.ts --bundle --outdir=dist --platform=neutral --external:react",
|
|
11
11
|
"demo": "npx @t8/serve 3000 * tests/counter -b src/index.tsx",
|
|
12
|
-
"gh-pages": "npx ghstage --theme=t8 --ymid=103784239 --nav=https://raw.githubusercontent.com/t8js/t8js.github.io/refs/heads/main/assets/
|
|
12
|
+
"gh-pages": "npx ghstage --theme=t8 --ymid=103784239 --nav=https://raw.githubusercontent.com/t8js/t8js.github.io/refs/heads/main/assets/nav_react.html --single-page",
|
|
13
13
|
"prepublishOnly": "npx npm-run-all build gh-pages",
|
|
14
14
|
"preversion": "npx npm-run-all typecheck shape build test",
|
|
15
15
|
"shape": "npx codeshape",
|