state-jet 2.0.7 → 2.0.9
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 +20 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
A zero-boilerplate, ultra-fast global state management library for React.
|
|
1
|
+
A zero-boilerplate, ultra-fast global state management library for React.
|
|
2
2
|
|
|
3
3
|
For more details, see [here](https://statejet.netlify.app).
|
|
4
4
|
|
|
@@ -41,7 +41,7 @@ Or if you're using `cdn`:
|
|
|
41
41
|
|
|
42
42
|
## Basic Example Usage
|
|
43
43
|
|
|
44
|
-
```
|
|
44
|
+
```tsx
|
|
45
45
|
import { useStateGlobal } from "state-jet";
|
|
46
46
|
|
|
47
47
|
const counter = useStateGlobal("counter", 0);
|
|
@@ -51,7 +51,7 @@ function Counter() {
|
|
|
51
51
|
return <button onClick={() => counter.set(count + 1)}>Count: {count}</button>;
|
|
52
52
|
}
|
|
53
53
|
```
|
|
54
|
-
##
|
|
54
|
+
## Why state-jet Is More Advanced Than Zustand
|
|
55
55
|
|
|
56
56
|
- **No Proxies Needed** → Zustand uses proxies for state updates, but state-jet uses signals, making it even faster.
|
|
57
57
|
- **Derived State Is Automatic** → No need for selectors; state updates only trigger where necessary.
|
|
@@ -65,30 +65,28 @@ If you need the simplest, fastest, and most advanced state management solution f
|
|
|
65
65
|
|
|
66
66
|
## Create Slice
|
|
67
67
|
|
|
68
|
-
```
|
|
68
|
+
```tsx
|
|
69
69
|
import { useSlice } from "state-jet";
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
export const useCartSlice = () =>
|
|
74
|
-
useSlice("cart")("items", []);
|
|
71
|
+
const productSlice = useSlice("products");
|
|
72
|
+
const cartSlice = useSlice("cart");
|
|
75
73
|
|
|
76
|
-
export const
|
|
74
|
+
export const useProductSlice = () => productSlice("list", []);
|
|
75
|
+
export const useCartSlice = () => cartSlice("list", []);
|
|
77
76
|
```
|
|
78
77
|
|
|
79
78
|
## Create Store
|
|
80
79
|
|
|
81
|
-
```
|
|
80
|
+
```tsx
|
|
82
81
|
import { useStore } from "state-jet";
|
|
83
|
-
import { useProductSlice, useCartSlice
|
|
82
|
+
import { useProductSlice, useCartSlice } from "./slices";
|
|
84
83
|
|
|
85
84
|
const initializer: any = () => ({
|
|
86
85
|
products: useProductSlice(),
|
|
87
86
|
cart: useCartSlice(),
|
|
88
|
-
user: useUserSlice()
|
|
89
87
|
});
|
|
90
88
|
|
|
91
|
-
export const
|
|
89
|
+
export const store = () => useStore(initializer);
|
|
92
90
|
```
|
|
93
91
|
|
|
94
92
|
## Middlewares
|
|
@@ -97,8 +95,8 @@ Unlike other libraries, you do not need to rely on any external dependencies. A
|
|
|
97
95
|
|
|
98
96
|
```bash
|
|
99
97
|
function useStateGlobal<T>(
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
...
|
|
99
|
+
options?: { middleware?: [] }
|
|
102
100
|
)
|
|
103
101
|
```
|
|
104
102
|
|
|
@@ -106,7 +104,7 @@ function useStateGlobal<T>(
|
|
|
106
104
|
|
|
107
105
|
You can log your store for every action.
|
|
108
106
|
|
|
109
|
-
```
|
|
107
|
+
```tsx
|
|
110
108
|
import { useStateGlobal } from "state-jet";
|
|
111
109
|
|
|
112
110
|
const loggerMiddleware = (key: string, prev: number, next: number) => {
|
|
@@ -132,7 +130,7 @@ export default function Counter() {
|
|
|
132
130
|
|
|
133
131
|
Can't live without reducer?. No worries. StateJet supports reducer middleware
|
|
134
132
|
|
|
135
|
-
```
|
|
133
|
+
```tsx
|
|
136
134
|
import { useStateGlobal } from "state-jet";
|
|
137
135
|
|
|
138
136
|
type Action<T> = { type: string; payload?: T };
|
|
@@ -176,7 +174,7 @@ export default function Counter() {
|
|
|
176
174
|
|
|
177
175
|
You can optimistically update global state with rollback support
|
|
178
176
|
|
|
179
|
-
```
|
|
177
|
+
```tsx
|
|
180
178
|
import { useStateGlobal } from "state-jet";
|
|
181
179
|
|
|
182
180
|
const optimisticMiddleware = (apiUrl: string) => {
|
|
@@ -196,7 +194,7 @@ const optimisticMiddleware = (apiUrl: string) => {
|
|
|
196
194
|
};
|
|
197
195
|
};
|
|
198
196
|
const profile = useStateGlobal("profile", { name: "John" }, {
|
|
199
|
-
|
|
197
|
+
middleware: [optimisticMiddleware("/update-profile")],
|
|
200
198
|
});
|
|
201
199
|
```
|
|
202
200
|
|
|
@@ -204,7 +202,7 @@ const profile = useStateGlobal("profile", { name: "John" }, {
|
|
|
204
202
|
|
|
205
203
|
You can create your own custom middleware in state-jet
|
|
206
204
|
|
|
207
|
-
```
|
|
205
|
+
```tsx
|
|
208
206
|
import { useStateGlobal } from "state-jet";
|
|
209
207
|
|
|
210
208
|
const validateAgeMiddleware = (key: string, prev: number, next: number) => {
|
|
@@ -224,7 +222,7 @@ export default function Profile() {
|
|
|
224
222
|
<h1>Age: {age}</h1>
|
|
225
223
|
<button
|
|
226
224
|
onClick={() => {
|
|
227
|
-
counter.set(-5) //Age will be 0 eventhough it updated with negative value due to middleware logic
|
|
225
|
+
counter.set(-5) // Age will be 0 eventhough it updated with negative value due to middleware logic
|
|
228
226
|
}}>
|
|
229
227
|
Set negative
|
|
230
228
|
</button>
|
|
@@ -239,7 +237,7 @@ A more complete middleware usage is [here](https://statejet.netlify.app/docs/api
|
|
|
239
237
|
|
|
240
238
|
Here is the example for creating global state with typescript definition.
|
|
241
239
|
|
|
242
|
-
```
|
|
240
|
+
```tsx
|
|
243
241
|
interface Todo = {
|
|
244
242
|
id: number;
|
|
245
243
|
text: string;
|