@t8/react-store 0.1.0 → 1.0.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 +108 -0
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/@t8/react-store)    
|
|
2
|
+
|
|
3
|
+
# @t8/react-store
|
|
4
|
+
|
|
5
|
+
*Straightforward and minimalist shared state management for React apps*
|
|
6
|
+
|
|
7
|
+
- Similar to `useState()`
|
|
8
|
+
- No boilerplate
|
|
9
|
+
- Painless transition from local state to shared state and vice versa
|
|
10
|
+
- SSR-compatible
|
|
11
|
+
|
|
12
|
+
Installation: `npm i @t8/react-store`
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
Moving the local state to the full-fledged shared state:
|
|
17
|
+
|
|
18
|
+
```diff
|
|
19
|
+
import {createContext, useContext} from 'react';
|
|
20
|
+
+ import {Store, useStore} from '@t8/react-store';
|
|
21
|
+
+
|
|
22
|
+
+ let AppContext = createContext(new Store(0));
|
|
23
|
+
|
|
24
|
+
let Counter = () => {
|
|
25
|
+
- let [counter, setCounter] = useState(0);
|
|
26
|
+
+ let [counter, setCounter] = useStore(useContext(AppContext));
|
|
27
|
+
|
|
28
|
+
let handleClick = () => {
|
|
29
|
+
setCounter(value => value + 1);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return <button onClick={handleClick}>{counter}</button>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
let ResetButton = () => {
|
|
36
|
+
- let [, setCounter] = useState(0);
|
|
37
|
+
+ let [, setCounter] = useStore(useContext(AppContext), false);
|
|
38
|
+
|
|
39
|
+
let handleClick = () => {
|
|
40
|
+
setCounter(0);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return <button onClick={handleClick}>×</button>;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
let App = () => <><Counter/>{' '}<ResetButton/></>;
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
[Live demo](https://codesandbox.io/p/sandbox/rtng37?file=%2Fsrc%2FPlusButton.jsx)
|
|
50
|
+
|
|
51
|
+
🔹 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.
|
|
52
|
+
|
|
53
|
+
🔹 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 the component doesn't make use of the store state value, but it may use the state setter.
|
|
54
|
+
|
|
55
|
+
🔹 An application can have as many stores as needed, whether on a single React Context or multiple Contexts.
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
let AppContext = createContext({
|
|
59
|
+
users: new Store(/* ... */),
|
|
60
|
+
items: new Store(/* ... */),
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
🔹 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:
|
|
65
|
+
|
|
66
|
+
```jsx
|
|
67
|
+
let ItemCard = ({id}) => {
|
|
68
|
+
let hasRelevantUpdates = useCallback((nextItems, prevItems) => {
|
|
69
|
+
return nextItems[id].revision !== prevItems[id].revision;
|
|
70
|
+
}, [id]);
|
|
71
|
+
|
|
72
|
+
let [items, setItems] = useStore(
|
|
73
|
+
useContext(AppContext).items,
|
|
74
|
+
hasRelevantUpdates,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
// content
|
|
79
|
+
);
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
🔹 Shared state can be provided to the app by means of a regular React Context provider:
|
|
84
|
+
|
|
85
|
+
```diff
|
|
86
|
+
let App = () => (
|
|
87
|
+
- <AppContext.Provider value={42}>
|
|
88
|
+
+ <AppContext.Provider value={new Store(42)}>
|
|
89
|
+
<PlusButton/>{' '}<Display/>
|
|
90
|
+
</AppContext.Provider>
|
|
91
|
+
);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
🔹 A store can contain data of any type.
|
|
95
|
+
|
|
96
|
+
Live demos:<br>
|
|
97
|
+
[Primitive value state](https://codesandbox.io/p/sandbox/rtng37?file=%2Fsrc%2FPlusButton.jsx)<br>
|
|
98
|
+
[Object value state](https://codesandbox.io/p/sandbox/y7wt2j?file=%2Fsrc%2FPlusButton.jsx)
|
|
99
|
+
|
|
100
|
+
🔹 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.
|
|
101
|
+
|
|
102
|
+
[Live demo with Immer](https://codesandbox.io/p/sandbox/rn4qsr?file=%2Fsrc%2FPlusButton.jsx)
|
|
103
|
+
|
|
104
|
+
🔹 A store initialized outside a component can be used as the component's remount-persistent state.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
- [Shared async action state tracking](https://github.com/t8dev/react-pending)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/react-store",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Straightforward and minimalist shared state management for React apps",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"build": "npx npm-run-all clean compile",
|
|
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
|
+
"gh-pages": "npx ghstage --color-scheme=mediumseagreen --ymid=103784239 --backstory=https://axtk.github.io/x/t8_react_store",
|
|
12
|
+
"prepublishOnly": "npx npm-run-all build gh-pages",
|
|
12
13
|
"preversion": "npx npm-run-all shape build",
|
|
13
14
|
"shape": "npx codeshape"
|
|
14
15
|
},
|
|
@@ -26,10 +27,10 @@
|
|
|
26
27
|
"peerDependencies": {
|
|
27
28
|
"react": ">=16.8"
|
|
28
29
|
},
|
|
29
|
-
"dependencies": {
|
|
30
|
-
"@t8/store": "^0.1.1"
|
|
31
|
-
},
|
|
32
30
|
"devDependencies": {
|
|
33
31
|
"@types/react": "^19.1.10"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@t8/store": "^1.0.0"
|
|
34
35
|
}
|
|
35
36
|
}
|