@geee-be/react-utils 1.1.1 → 1.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @geee-be/react-utils
2
2
 
3
+ ## 1.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 7c9d2da: Try to avoid hydration error
8
+
3
9
  ## 1.1.0
4
10
 
5
11
  ### Minor Changes
@@ -1,13 +1,19 @@
1
1
  'use client';
2
2
  import { useEffect, useState } from 'react';
3
3
  import { deserialize, getInitialValue, serialize } from './state.util.js';
4
+ const LOADING = Symbol('loading');
4
5
  export const useHistoryState = (key, initialValue, replace = true, options) => {
5
6
  if (typeof history === 'undefined') {
6
- return [undefined, () => { }];
7
+ return [undefined, () => { }, false];
7
8
  }
8
- const [value, setValue] = useState(() => deserialize(history.state?.[key], options?.fromSerializable) ??
9
- getInitialValue(initialValue));
9
+ const [value, setValue] = useState(LOADING);
10
10
  useEffect(() => {
11
+ setValue(deserialize(history.state?.[key], options?.fromSerializable) ??
12
+ getInitialValue(initialValue));
13
+ }, [key, options?.fromSerializable, initialValue]);
14
+ useEffect(() => {
15
+ if (value === LOADING)
16
+ return;
11
17
  if (replace) {
12
18
  history.replaceState({ ...history.state, [key]: serialize(value, options?.toSerializable) }, '');
13
19
  }
@@ -15,5 +21,12 @@ export const useHistoryState = (key, initialValue, replace = true, options) => {
15
21
  history.pushState({ ...history.state, [key]: serialize(value, options?.toSerializable) }, '');
16
22
  }
17
23
  }, [key, value, options?.toSerializable, replace]);
18
- return [value, setValue];
24
+ if (value === LOADING) {
25
+ return [undefined, () => { }, false];
26
+ }
27
+ return [
28
+ value,
29
+ (v) => setValue(v),
30
+ true,
31
+ ];
19
32
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geee-be/react-utils",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "license": "MIT",
@@ -34,6 +34,7 @@
34
34
  "build-storybook": "storybook build",
35
35
  "lint": "npm run lint:biome && npm run lint:types && npm run lint:unused-exports",
36
36
  "lint:biome": "biome ci src/",
37
+ "lint:biome:fix": "biome check src/ --apply",
37
38
  "lint:types": "tsc --noEmit",
38
39
  "lint:unused-exports": "ts-unused-exports tsconfig.unused-exports.json --excludePathsFromReport=src/index.ts",
39
40
  "prestart:dev": "rimraf dist/*",
@@ -5,22 +5,34 @@ import { useEffect, useState } from 'react';
5
5
  import type { InitialValue, SerializationOptions } from './state.util.js';
6
6
  import { deserialize, getInitialValue, serialize } from './state.util.js';
7
7
 
8
+ type Response<T> =
9
+ | [T, Dispatch<SetStateAction<T>>, true]
10
+ | [undefined, Dispatch<SetStateAction<T>>, false];
11
+
12
+ const LOADING = Symbol('loading');
13
+
8
14
  export const useHistoryState = <T, S = T>(
9
15
  key: string,
10
16
  initialValue: InitialValue<T>,
11
17
  replace = true,
12
18
  options?: SerializationOptions<T, S>,
13
- ): [T, Dispatch<SetStateAction<T>>] => {
19
+ ): Response<T> => {
14
20
  if (typeof history === 'undefined') {
15
- return [undefined as any, () => {}];
21
+ return [undefined, () => {}, false];
16
22
  }
17
- const [value, setValue] = useState<T>(
18
- () =>
23
+
24
+ const [value, setValue] = useState<T | typeof LOADING>(LOADING);
25
+
26
+ useEffect(() => {
27
+ setValue(
19
28
  deserialize(history.state?.[key], options?.fromSerializable) ??
20
- getInitialValue(initialValue),
21
- );
29
+ getInitialValue(initialValue),
30
+ );
31
+ }, [key, options?.fromSerializable, initialValue]);
22
32
 
23
33
  useEffect(() => {
34
+ if (value === LOADING) return;
35
+
24
36
  if (replace) {
25
37
  history.replaceState(
26
38
  { ...history.state, [key]: serialize(value, options?.toSerializable) },
@@ -34,5 +46,13 @@ export const useHistoryState = <T, S = T>(
34
46
  }
35
47
  }, [key, value, options?.toSerializable, replace]);
36
48
 
37
- return [value, setValue];
49
+ if (value === LOADING) {
50
+ return [undefined, () => {}, false];
51
+ }
52
+
53
+ return [
54
+ value,
55
+ (v) => setValue(v as SetStateAction<T | typeof LOADING>),
56
+ true,
57
+ ];
38
58
  };