state-in-url 4.1.3 → 4.1.5

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
@@ -22,6 +22,7 @@ English | [한국어](./README.KO.md) | [简体中文](./README.CN.md)
22
22
 
23
23
  [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/asmyshlyaev177/state-in-url/badge)](https://scorecard.dev/viewer/?uri=github.com/asmyshlyaev177/state-in-url)
24
24
  [![OpenSSF Best Practices](https://www.bestpractices.dev/projects/9679/badge)](https://www.bestpractices.dev/projects/9679)
25
+ [![license](https://img.shields.io/github/license/asmyshlyaev177/state-in-url.svg?style=flat-square)](https://github.com/asmyshlyaev177/state-in-url/blob/master/LICENSE)
25
26
  <!-- or by embedding this in your HTML:
26
27
  <a href="https://www.bestpractices.dev/projects/9679"><img src="https://www.bestpractices.dev/projects/9679/badge"></a> -->
27
28
 
@@ -33,8 +34,10 @@ English | [한국어](./README.KO.md) | [简体中文](./README.CN.md)
33
34
 
34
35
  ![Demo-gif](https://github.com/asmyshlyaev177/state-in-url/blob/master/assets/Demo-gif.gif)
35
36
 
36
- <a href="https://state-in-url.dev" target="_blank">DEMO</a> |
37
- <a href="https://state-in-url.netlify.app/" target="_blank">DEMO2</a>
37
+ # Demo
38
+
39
+ <a href="https://state-in-url.dev" target="_blank">Demo</a> |
40
+ <a href="https://state-in-url.netlify.app/" target="_blank">Mirror link</a>
38
41
 
39
42
  <a href="https://github.com/asmyshlyaev177/state-in-url/blob/master/Limits.md" target="_blank">URI size limitation, <b>up to 12KB</b> is safe</a>
40
43
 
@@ -82,12 +85,13 @@ This library is a good alternative for NUQS.
82
85
  - ✨ **Complex data**: Nested objects, dates and arrays, works same as JSON, but in URL
83
86
  - ☂ **Default values**: Giving you default values if parameter not in url
84
87
  - ⌨ **Organized**: All possible values defined at start, protect you from getting non existing key
88
+ - **flexible**: Can use more than 1 state objects on the same page, just use different keys
85
89
  - **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
86
90
  - **Server Side Rendering**: Can use it in Server Components, Next.js 14 and 15 are supported
87
91
  - **Lightweight**: Zero dependencies, library less than 2KB
88
92
  - **DX**: Good developer experience, documentation, JSDoc comments, and examples
89
93
  - **Framework Flexibility**: Hooks for `Next.js` and `react-router`, helpers to use it with other frameworks or pure JS
90
- - **Well tested**: Unit tests and Playwright tests for Chrome/Firefox/Safari
94
+ - **Well tested**: [Unit tests and Playwright tests for Chrome/Firefox/Safari](https://github.com/asmyshlyaev177/state-in-url/actions/workflows/tests.yml)
91
95
  - **Permissive license**: MIT
92
96
 
93
97
  ## Table of content
@@ -592,7 +596,14 @@ const userState = {
592
596
  };
593
597
 
594
598
  export const useUserState = () => {
595
- const { urlState, setUrl } = useUrlState(userState);
599
+ const { urlState, setUrl, reset } = useUrlState(userState);
600
+
601
+ // other logic
602
+
603
+ // reset query params when navigating to other page
604
+ React.useEffect(() => {
605
+ return reset
606
+ }, [])
596
607
 
597
608
  return { userState: urlState, setUserState: setUrl };;
598
609
  }
@@ -7,10 +7,19 @@ import { type JSONCompatible } from "../../utils";
7
7
  * .
8
8
  */
9
9
  export declare function useUrlState<T extends JSONCompatible>({ defaultState: T, searchParams, replace, scroll, useHistory, }: OldParams<T>): {
10
+ /**
11
+ * @deprecated use `urlState`
12
+ */
10
13
  state: T;
11
14
  urlState: T;
15
+ /**
16
+ * @deprecated use `setState`
17
+ */
12
18
  updateState: (value: Partial<T> | ((currState: T) => T)) => void;
13
19
  setState: (value: Partial<T> | ((currState: T) => T)) => void;
20
+ /**
21
+ * @deprecated use `setUrl`
22
+ */
14
23
  updateUrl: (value?: Partial<T> | ((currState: T) => T)) => void;
15
24
  setUrl: (value?: Partial<T> | ((currState: T) => T)) => void;
16
25
  };
@@ -49,9 +58,42 @@ export declare function useUrlState<T extends JSONCompatible>({ defaultState: T,
49
58
  * * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/next/useUrlState}
50
59
  */
51
60
  export declare function useUrlState<T extends JSONCompatible>(defaultState: T, params?: Params): {
61
+ /**
62
+ * State object. Don't mutate directly, use `setState` or `setUrl`
63
+ */
52
64
  urlState: T;
65
+ /**
66
+ * * Example:
67
+ * ```ts
68
+ * setState({ name: 'test' });
69
+ * // or
70
+ * setState(curr => ({ ...curr, name: 'test' }) );
71
+ * ```
72
+ *
73
+ * * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/next/useUrlState#updatestate}
74
+ */
53
75
  setState: (value: Partial<T> | ((currState: T) => T)) => void;
76
+ /**
77
+ * * Example:
78
+ * ```ts
79
+ * setUrl({ name: 'test' });
80
+ * // or
81
+ * setUrl(curr => ({ ...curr, name: 'test' }), { replace: true, scroll: false } );
82
+ * ```
83
+ *
84
+ * * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/next/useUrlState#updateurl}
85
+ */
54
86
  setUrl: (value?: Partial<T> | ((currState: T) => T), options?: Options) => void;
87
+ /**
88
+ * * Example:
89
+ * ```ts
90
+ * reset();
91
+ * // or
92
+ * reset({ replace: false, scroll: true })
93
+ * ```
94
+ *
95
+ * * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/next/useUrlState#reset}
96
+ */
55
97
  reset: (options?: Options & {
56
98
  [key: string]: unknown;
57
99
  }) => void;
@@ -7,8 +7,14 @@ import { type JSONCompatible } from "../../utils";
7
7
  * .
8
8
  */
9
9
  export declare function useUrlState<T extends JSONCompatible>({ defaultState: T, searchParams, replace, useHistory, }: OldParams<T>): {
10
+ /**
11
+ * @deprecated use `urlState`
12
+ */
10
13
  state: T;
11
14
  urlState: T;
15
+ /**
16
+ * @deprecated use `setState`
17
+ */
12
18
  updateState: (value: Partial<T> | ((currState: T) => T)) => void;
13
19
  setState: (value: Partial<T> | ((currState: T) => T)) => void;
14
20
  updateUrl: (value?: Partial<T> | ((currState: T) => T)) => void;
@@ -46,9 +52,46 @@ export declare function useUrlState<T extends JSONCompatible>({ defaultState: T,
46
52
  * * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/react-router/useUrlState}
47
53
  */
48
54
  export declare function useUrlState<T extends JSONCompatible>(defaultState: T, params?: Params): {
55
+ /**
56
+ * State object. Don't mutate directly, use `setState` or `setUrl`
57
+ */
49
58
  urlState: T;
59
+ /**
60
+ * * Example:
61
+ * ```ts
62
+ * setState({ name: 'test' });
63
+ * // or
64
+ * setState(curr => ({ ...curr, name: 'test' }) );
65
+ * // can pass optional React-Router `NavigateOptions`
66
+ * setState(curr => ({ ...curr, name: 'test' }) );
67
+ * ```
68
+ *
69
+ * * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/react-router/useUrlState#updatestate}
70
+ */
50
71
  setState: (value: Partial<T> | ((currState: T) => T)) => void;
72
+ /**
73
+ * * Example:
74
+ * ```ts
75
+ * setUrl({ name: 'test' });
76
+ * // or
77
+ * setUrl(curr => ({ ...curr, name: 'test' }), { replace: true } );
78
+ * // can pass optional React-Router `NavigateOptions`
79
+ * setUrl(curr => ({ ...curr, name: 'test' }), { preventScrollReset: false } );
80
+ * ```
81
+ *
82
+ * * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/react-router/useUrlState#updateurl}
83
+ */
51
84
  setUrl: (value?: Partial<T> | ((currState: T) => T), options?: Params) => void;
85
+ /**
86
+ * * Example:
87
+ * ```ts
88
+ * reset();
89
+ * // or
90
+ * reset({ replace: false, preventScrollReset: false })
91
+ * ```
92
+ *
93
+ * * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/react-router/useUrlState#reset}
94
+ */
52
95
  reset: (options?: NavigateOptions & {
53
96
  [key: string]: unknown;
54
97
  }) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "state-in-url",
3
- "version": "4.1.3",
3
+ "version": "4.1.5",
4
4
  "description": "Easily share complex state objects between unrelated React components, preserve types and structure, with TS validation. Deep links and url state synchronization wthout any hasssle or boilerplate.",
5
5
  "homepage": "https://state-in-url.dev",
6
6
  "repository": {