@t8/react-store 1.0.27 → 1.0.29
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 +43 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,10 +4,26 @@
|
|
|
4
4
|
|
|
5
5
|
*Concise shared state management for React apps*
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
🔹 Similar to `useState()`
|
|
8
|
+
|
|
9
|
+
```diff
|
|
10
|
+
+ let store = new Store(0);
|
|
11
|
+
|
|
12
|
+
let Counter = () => {
|
|
13
|
+
- let [counter, setCounter] = useState(0);
|
|
14
|
+
+ let [counter, setCounter] = useStore(store);
|
|
15
|
+
|
|
16
|
+
let handleClick = () => {
|
|
17
|
+
setCounter(value => value + 1);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return <button onClick={handleClick}>+ {counter}</button>;
|
|
21
|
+
};
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
🔹 No boilerplate<br>
|
|
25
|
+
🔹 Painless transition from local state<br>
|
|
26
|
+
🔹 SSR- and CSR-compatible
|
|
11
27
|
|
|
12
28
|
Installation: `npm i @t8/react-store`
|
|
13
29
|
|
|
@@ -71,6 +87,8 @@ let [, setState] = useState(store, false);
|
|
|
71
87
|
Apart from a boolean, `useStore(store, shouldUpdate)` accepts a function of `(nextState, prevState) => boolean` as the second parameter to filter store updates to respond to:
|
|
72
88
|
|
|
73
89
|
```jsx
|
|
90
|
+
import { useStore } from "@t8/react-store";
|
|
91
|
+
|
|
74
92
|
let ItemCard = ({ id }) => {
|
|
75
93
|
// Definition of changes in the item
|
|
76
94
|
let hasRelevantUpdates = useCallback((nextItems, prevItems) => {
|
|
@@ -90,41 +108,43 @@ let ItemCard = ({ id }) => {
|
|
|
90
108
|
|
|
91
109
|
Shared state can be provided to the app by means of a regular React Context provider:
|
|
92
110
|
|
|
93
|
-
```
|
|
94
|
-
import { createContext } from "react";
|
|
111
|
+
```diff
|
|
112
|
+
+ import { createContext, useContext } from "react";
|
|
113
|
+
import { Store, useStore } from "@t8/react-store";
|
|
95
114
|
|
|
96
|
-
|
|
97
|
-
|
|
115
|
+
- let counterStore = new Store(0);
|
|
116
|
+
+ let AppContext = createContext(new Store(0));
|
|
98
117
|
|
|
99
|
-
|
|
100
|
-
let
|
|
101
|
-
|
|
102
|
-
<PlusButton/>{" "}<Display/>
|
|
103
|
-
</AppContext.Provider>
|
|
104
|
-
);
|
|
105
|
-
```
|
|
118
|
+
let Counter = () => {
|
|
119
|
+
- let [counter, setCounter] = useStore(counterStore);
|
|
120
|
+
+ let [counter, setCounter] = useStore(useContext(AppContext));
|
|
106
121
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
let [counter, setCounter] = useStore(useContext(AppContext));
|
|
122
|
+
// Rendering
|
|
123
|
+
};
|
|
110
124
|
|
|
111
|
-
|
|
112
|
-
|
|
125
|
+
let App = () => (
|
|
126
|
+
- <>
|
|
127
|
+
+ <AppContext.Provider value={new Store(42)}>
|
|
128
|
+
<PlusButton/>{" "}<Display/>
|
|
129
|
+
+ </AppContext.Provider>
|
|
130
|
+
- </>
|
|
131
|
+
);
|
|
113
132
|
```
|
|
114
133
|
|
|
115
134
|
[Live counter demo with Context](https://codesandbox.io/p/sandbox/rtng37?file=%2Fsrc%2FPlusButton.jsx)
|
|
116
135
|
|
|
117
136
|
🔹 In a multi-store setup, stores can be located in a single Context or split across multiple Contexts, just like any application data.
|
|
118
137
|
|
|
119
|
-
```
|
|
138
|
+
```jsx
|
|
139
|
+
import { createContext, useContext } from "react";
|
|
140
|
+
import { Store, useStore } from "@t8/react-store";
|
|
141
|
+
|
|
120
142
|
// Multiple stores in a single Context
|
|
121
143
|
let AppContext = createContext({
|
|
122
144
|
users: new Store(/* ... */),
|
|
123
145
|
items: new Store(/* ... */),
|
|
124
146
|
});
|
|
125
|
-
```
|
|
126
147
|
|
|
127
|
-
```jsx
|
|
128
148
|
let ItemCard = ({ id }) => {
|
|
129
149
|
let [items, setItems] = useStore(useContext(AppContext).items);
|
|
130
150
|
|