zustand-querystring 0.0.17 → 0.0.18
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 +10 -7
- package/dist/{chunk-4CDMBGCR.mjs → chunk-H7YBN6IK.mjs} +7 -7
- package/dist/index.js +7 -7
- package/dist/index.mjs +1 -1
- package/dist/middleware.js +7 -7
- package/dist/middleware.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,14 +5,16 @@ A Zustand middleware that syncs the store with the querystring.
|
|
|
5
5
|
Try on [StackBlitz](https://stackblitz.com/github/nitedani/zustand-querystring/tree/main/examples/react) (You need to click "Open in New Tab")
|
|
6
6
|
|
|
7
7
|
Examples:
|
|
8
|
+
|
|
8
9
|
- [React](./examples/react/)
|
|
9
10
|
- [NextJS](./examples/next/)
|
|
10
11
|
- [Rakkas](./examples/rakkas/)
|
|
11
12
|
|
|
12
13
|
Quickstart:
|
|
14
|
+
|
|
13
15
|
```ts
|
|
14
|
-
import create from
|
|
15
|
-
import { querystring } from
|
|
16
|
+
import create from 'zustand';
|
|
17
|
+
import { querystring } from 'zustand-querystring';
|
|
16
18
|
|
|
17
19
|
interface Store {
|
|
18
20
|
count: number;
|
|
@@ -30,7 +32,7 @@ export const useStore = create<Store>()(
|
|
|
30
32
|
ticks: 0,
|
|
31
33
|
someNestedState: {
|
|
32
34
|
nestedCount: 0,
|
|
33
|
-
hello:
|
|
35
|
+
hello: 'Hello',
|
|
34
36
|
},
|
|
35
37
|
}),
|
|
36
38
|
{
|
|
@@ -43,19 +45,20 @@ export const useStore = create<Store>()(
|
|
|
43
45
|
|
|
44
46
|
someNestedState: {
|
|
45
47
|
nestedCount: true,
|
|
46
|
-
hello:
|
|
48
|
+
hello: '/about' === pathname,
|
|
47
49
|
},
|
|
48
50
|
|
|
49
51
|
// OR select the whole nested state
|
|
50
52
|
// someNestedState: true
|
|
51
53
|
};
|
|
52
54
|
},
|
|
53
|
-
}
|
|
54
|
-
)
|
|
55
|
+
},
|
|
56
|
+
),
|
|
55
57
|
);
|
|
56
58
|
```
|
|
57
59
|
|
|
58
60
|
querystring options:
|
|
61
|
+
|
|
59
62
|
- <b>select</b> - the select option controls what part of the state is synced with the query string
|
|
60
63
|
- <b>key: string</b> - the key option controls how the state is stored in the querystring (default: $)
|
|
61
|
-
- <b>url</b> - the url option is used to provide the request url on the server side render
|
|
64
|
+
- <b>url</b> - the url option is used to provide the request url on the server side render
|
|
@@ -127,10 +127,10 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
127
127
|
ignored += (ignored ? "&" : "?") + str;
|
|
128
128
|
}
|
|
129
129
|
const newCompacted = compact(newMerged, initialState);
|
|
130
|
+
let newQueryString = "";
|
|
130
131
|
if (Object.keys(newCompacted).length) {
|
|
131
132
|
const stringified = stringify(newCompacted);
|
|
132
133
|
const newQueryState = `${defaultedOptions.key}=${stringified};;`;
|
|
133
|
-
let newQueryString = "";
|
|
134
134
|
if (currentParsed) {
|
|
135
135
|
newQueryString = currentQueryString.replace(
|
|
136
136
|
splitMatcher,
|
|
@@ -141,13 +141,13 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
141
141
|
} else {
|
|
142
142
|
newQueryString = "?" + newQueryState;
|
|
143
143
|
}
|
|
144
|
-
history.replaceState(
|
|
145
|
-
history.state,
|
|
146
|
-
"",
|
|
147
|
-
location.pathname + newQueryString
|
|
148
|
-
);
|
|
149
144
|
} else {
|
|
150
|
-
|
|
145
|
+
newQueryString = ignored;
|
|
146
|
+
}
|
|
147
|
+
const currentUrl = location.pathname + location.search;
|
|
148
|
+
const newUrl = location.pathname + newQueryString;
|
|
149
|
+
if (newUrl !== currentUrl) {
|
|
150
|
+
history.replaceState(history.state, "", newUrl);
|
|
151
151
|
}
|
|
152
152
|
};
|
|
153
153
|
if (!api.__ZUSTAND_QUERYSTRING_INIT__) {
|
package/dist/index.js
CHANGED
|
@@ -270,10 +270,10 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
270
270
|
ignored += (ignored ? "&" : "?") + str;
|
|
271
271
|
}
|
|
272
272
|
const newCompacted = compact(newMerged, initialState);
|
|
273
|
+
let newQueryString = "";
|
|
273
274
|
if (Object.keys(newCompacted).length) {
|
|
274
275
|
const stringified = stringify(newCompacted);
|
|
275
276
|
const newQueryState = `${defaultedOptions.key}=${stringified};;`;
|
|
276
|
-
let newQueryString = "";
|
|
277
277
|
if (currentParsed) {
|
|
278
278
|
newQueryString = currentQueryString.replace(
|
|
279
279
|
splitMatcher,
|
|
@@ -284,13 +284,13 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
284
284
|
} else {
|
|
285
285
|
newQueryString = "?" + newQueryState;
|
|
286
286
|
}
|
|
287
|
-
history.replaceState(
|
|
288
|
-
history.state,
|
|
289
|
-
"",
|
|
290
|
-
location.pathname + newQueryString
|
|
291
|
-
);
|
|
292
287
|
} else {
|
|
293
|
-
|
|
288
|
+
newQueryString = ignored;
|
|
289
|
+
}
|
|
290
|
+
const currentUrl = location.pathname + location.search;
|
|
291
|
+
const newUrl = location.pathname + newQueryString;
|
|
292
|
+
if (newUrl !== currentUrl) {
|
|
293
|
+
history.replaceState(history.state, "", newUrl);
|
|
294
294
|
}
|
|
295
295
|
};
|
|
296
296
|
if (!api.__ZUSTAND_QUERYSTRING_INIT__) {
|
package/dist/index.mjs
CHANGED
package/dist/middleware.js
CHANGED
|
@@ -267,10 +267,10 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
267
267
|
ignored += (ignored ? "&" : "?") + str;
|
|
268
268
|
}
|
|
269
269
|
const newCompacted = compact(newMerged, initialState);
|
|
270
|
+
let newQueryString = "";
|
|
270
271
|
if (Object.keys(newCompacted).length) {
|
|
271
272
|
const stringified = stringify(newCompacted);
|
|
272
273
|
const newQueryState = `${defaultedOptions.key}=${stringified};;`;
|
|
273
|
-
let newQueryString = "";
|
|
274
274
|
if (currentParsed) {
|
|
275
275
|
newQueryString = currentQueryString.replace(
|
|
276
276
|
splitMatcher,
|
|
@@ -281,13 +281,13 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
281
281
|
} else {
|
|
282
282
|
newQueryString = "?" + newQueryState;
|
|
283
283
|
}
|
|
284
|
-
history.replaceState(
|
|
285
|
-
history.state,
|
|
286
|
-
"",
|
|
287
|
-
location.pathname + newQueryString
|
|
288
|
-
);
|
|
289
284
|
} else {
|
|
290
|
-
|
|
285
|
+
newQueryString = ignored;
|
|
286
|
+
}
|
|
287
|
+
const currentUrl = location.pathname + location.search;
|
|
288
|
+
const newUrl = location.pathname + newQueryString;
|
|
289
|
+
if (newUrl !== currentUrl) {
|
|
290
|
+
history.replaceState(history.state, "", newUrl);
|
|
291
291
|
}
|
|
292
292
|
};
|
|
293
293
|
if (!api.__ZUSTAND_QUERYSTRING_INIT__) {
|
package/dist/middleware.mjs
CHANGED