state-in-url 4.1.7 → 4.2.0
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
CHANGED
|
@@ -85,6 +85,7 @@ This library is a good alternative for NUQS.
|
|
|
85
85
|
- ✨ **Complex data**: Nested objects, dates and arrays, works same as JSON, but in URL
|
|
86
86
|
- ☂ **Default values**: Giving you default values if parameter not in url
|
|
87
87
|
- ⌨ **Organized**: All possible values defined at start, protect you from getting non existing key
|
|
88
|
+
- **compatible**: Will keep 3rd party query params as is
|
|
88
89
|
- **flexible**: Can use more than 1 state objects on the same page, just use different keys
|
|
89
90
|
- **Fast**: Minimal rerenders, around [1ms](https://github.com/asmyshlyaev177/state-in-url/blob/87c8c7c995c5cd7d9e7aa039c30bfe64b24abe4b/packages/urlstate/encoder/encoder.test.ts#L185) to encode and decode big object
|
|
90
91
|
- **Server Side Rendering**: Can use it in Server Components, Next.js 14 and 15 are supported
|
|
@@ -170,7 +171,7 @@ import { userState } from './userState';
|
|
|
170
171
|
|
|
171
172
|
function MyComponent() {
|
|
172
173
|
// can pass `replace` arg, it's control will `setUrl` will use `rounter.push` or `router.replace`, default replace=true
|
|
173
|
-
// can pass `searchParams` from server components
|
|
174
|
+
// can pass `searchParams` from server components, pass `useHistory: false` if you need to fetch smt in the server component
|
|
174
175
|
const { urlState, setUrl, setState, reset } = useUrlState(userState);
|
|
175
176
|
|
|
176
177
|
return (
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useRouter as
|
|
1
|
+
import l from"react";import{useRouter as C}from"next/navigation";import{parseSPObj as b}from"../../parseSPObj.mjs";import{useUrlStateBase as y}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as S,isSSR as H,filterUnknownParams as g,filterUnknownParamsClient as v}from"../../utils.mjs";function w(t,a){const r="defaultState"in t?t.defaultState:t,f="defaultState"in t?t.searchParams:a?.searchParams,s="defaultState"in t?t.useHistory:a?.useHistory,d="defaultState"in t?{scroll:t.scroll,replace:t.replace}:{scroll:a?.scroll,replace:a?.replace},o=C(),U=l.useMemo(()=>({push:(...e)=>{s===void 0||s?S.push(...e):o.push(...e)},replace:(...e)=>{s===void 0||s?S.replace(...e):o.replace(...e)}}),[o]),{state:u,updateState:p,updateUrl:c,reset:i,getState:h}=y(r,U,({parse:e})=>H?b(g(r,f),r):e(v(r))),m=l.useMemo(()=>({...M,...d}),[]),n=l.useCallback((e,k)=>c(e,{...m,...k}),[c]),P=l.useCallback(e=>{i({...m,...e})},[i]);return{setState:p,updateState:p,setUrl:n,updateUrl:n,urlState:u,state:u,reset:P,getState:h}}const M={replace:!0,scroll:!1};export{w as useUrlState};
|
package/dist/subscribers.d.ts
CHANGED
|
@@ -6,6 +6,6 @@ export declare const subscribers: {
|
|
|
6
6
|
};
|
|
7
7
|
export declare const stateMap: {
|
|
8
8
|
get: <T extends JSONCompatible | undefined>(objKey: T) => T | undefined;
|
|
9
|
-
set: <
|
|
9
|
+
set: <T_1 extends JSONCompatible>(objKey: T_1, value: T_1) => void;
|
|
10
10
|
};
|
|
11
11
|
export type Cb = () => void;
|
|
@@ -30,7 +30,7 @@ console.log(state); // Output: { name: 'Tom' }
|
|
|
30
30
|
*
|
|
31
31
|
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/useUrlEncode#parse}
|
|
32
32
|
*/
|
|
33
|
-
parse: (strOrSearchParams: string | URLSearchParams) =>
|
|
33
|
+
parse: (strOrSearchParams: string | URLSearchParams) => T;
|
|
34
34
|
/**
|
|
35
35
|
Converts a state object to a URL query string.
|
|
36
36
|
* * Example:
|
|
@@ -37,7 +37,7 @@ export declare function useUrlStateBase<T extends JSONCompatible>(defaultState:
|
|
|
37
37
|
parse: ReturnType<typeof useUrlEncode<T>>["parse"];
|
|
38
38
|
}) => T): {
|
|
39
39
|
updateState: (value: Partial<T> | ((currState: T) => T)) => void;
|
|
40
|
-
updateUrl: (value?:
|
|
40
|
+
updateUrl: (value?: Partial<T> | ((currState: T) => T) | undefined, options?: Options) => void;
|
|
41
41
|
state: T;
|
|
42
42
|
reset: (options?: Options) => void;
|
|
43
43
|
getState: () => T;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import i from"react";import{TIMEOUT as R}from"../constants/constants.mjs";import{useInsertionEffect as T}from"../useInsertionEffect.mjs";import{useSharedState as v}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as L}from"../useUrlEncode/useUrlEncode.mjs";import{filterUnknownParamsClient as P}from"../utils.mjs";function b(t,o,u){const{parse:a,stringify:n}=L(t),{state:l,getState:r,setState:s}=v(t,()=>u?.({parse:a})||t),m=i.useRef(void 0);T(()=>{const e=()=>{const w=a(P(t));s(w)};return window.addEventListener(U,e),()=>{window.removeEventListener(U,e),clearTimeout(m.current)}},[s]);const c=i.useRef([]),p=i.useCallback((e,w)=>{const d=typeof e=="function"?e(r()):{...r(),...e},h=n(d,y(t)),S=`${window.location.pathname}${h.length?"?":""}${h}${window.location.hash}`,$=`${window.location.pathname}${window.location.search}${window.location.hash}`;if(S===$)return;s(d);const{replace:g,...k}=w||{};c.current.push([g?"replace":"push",S,k]),clearTimeout(m.current),queueMicrotask(()=>{c.current.length&&(m.current=setTimeout(()=>{const f=c.current.at(-1);c.current=[],o[f[0]](f[1],f[2])},R))})},[o,n,r]),E=i.useCallback(e=>{p(t,e)},[p]);return{updateState:s,updateUrl:p,state:l,reset:E,getState:r}}function y(t){const o=Object.keys(t),u=window.location.search,a=new URLSearchParams(u),n=new URLSearchParams;return a.forEach((l,r)=>!o.includes(r)&&n.set(r,l)),n}const U="popstate";export{b as useUrlStateBase};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "state-in-url",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Store state in URL as in object, types and structure are preserved, with TS validation. Same API as React.useState, wthout any hasssle or boilerplate. Next.js@14-15 and react-router@6-7.",
|
|
5
5
|
"homepage": "https://state-in-url.dev",
|
|
6
6
|
"repository": {
|
|
@@ -14,8 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"publishConfig": {
|
|
16
16
|
"registry": "https://registry.npmjs.org/",
|
|
17
|
-
"tag": "latest"
|
|
18
|
-
"provenance": true
|
|
17
|
+
"tag": "latest"
|
|
19
18
|
},
|
|
20
19
|
"license": "MIT",
|
|
21
20
|
"funding": {
|
|
@@ -364,6 +363,7 @@
|
|
|
364
363
|
"eslint-plugin-maintainable": "^0.4.4",
|
|
365
364
|
"eslint-plugin-prettier": "^5.2.1",
|
|
366
365
|
"eslint-plugin-react-hooks": "^4.6.2",
|
|
366
|
+
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
367
367
|
"happy-dom": "^15.11.6",
|
|
368
368
|
"husky": "^9.0.11",
|
|
369
369
|
"lint-staged": "^15.2.9",
|