zustand-querystring 0.0.1 → 0.0.2
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 +76 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# zustand-querystring
|
|
2
|
+
|
|
3
|
+
A zustand middleware that stores state in the querystring.
|
|
4
|
+
|
|
5
|
+
Quickstart:
|
|
6
|
+
```ts
|
|
7
|
+
import create from "zustand";
|
|
8
|
+
import { immer } from "zustand/middleware/immer";
|
|
9
|
+
import { queryString } from "zustand-querystring";
|
|
10
|
+
|
|
11
|
+
interface Store {
|
|
12
|
+
count: number;
|
|
13
|
+
incrementCount: () => void;
|
|
14
|
+
|
|
15
|
+
ticks: number;
|
|
16
|
+
incrementTicks: () => void;
|
|
17
|
+
|
|
18
|
+
someNestedState: {
|
|
19
|
+
nestedCount: number;
|
|
20
|
+
incrementNestedCount: () => void;
|
|
21
|
+
|
|
22
|
+
hello: string;
|
|
23
|
+
setHello: (hello: string) => void;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const useStore = create<Store>()(
|
|
28
|
+
queryString(
|
|
29
|
+
immer((set, get) => ({
|
|
30
|
+
count: 0,
|
|
31
|
+
incrementCount: () =>
|
|
32
|
+
set((state) => {
|
|
33
|
+
state.count += 1;
|
|
34
|
+
}),
|
|
35
|
+
|
|
36
|
+
ticks: 0,
|
|
37
|
+
incrementTicks: () =>
|
|
38
|
+
set((state) => {
|
|
39
|
+
state.ticks += 1;
|
|
40
|
+
}),
|
|
41
|
+
|
|
42
|
+
someNestedState: {
|
|
43
|
+
nestedCount: 0,
|
|
44
|
+
incrementNestedCount: () =>
|
|
45
|
+
set((state) => {
|
|
46
|
+
state.someNestedState.nestedCount += 1;
|
|
47
|
+
}),
|
|
48
|
+
|
|
49
|
+
hello: "Hello",
|
|
50
|
+
setHello: (hello: string) =>
|
|
51
|
+
set((state) => {
|
|
52
|
+
state.someNestedState.hello = hello;
|
|
53
|
+
}),
|
|
54
|
+
},
|
|
55
|
+
})),
|
|
56
|
+
{
|
|
57
|
+
// select controls what part of the state is synced with the query string
|
|
58
|
+
// pathname is the current route (e.g. /about or /)
|
|
59
|
+
select(pathname) {
|
|
60
|
+
return {
|
|
61
|
+
count: true,
|
|
62
|
+
// ticks: false, <- false by default, because we specified the parent key
|
|
63
|
+
|
|
64
|
+
someNestedState: {
|
|
65
|
+
nestedCount: true,
|
|
66
|
+
hello: "/about" === pathname,
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
// OR select the whole nested state
|
|
70
|
+
// someNestedState: true
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
}
|
|
74
|
+
)
|
|
75
|
+
);
|
|
76
|
+
```
|