File tree 2 files changed +19
-2
lines changed
packages/hooks/src/useLocalStorageState
2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -26,18 +26,27 @@ A Hook that store state into localStorage.
26
26
If you want to delete this record from localStorage, you can use ` setState() ` or ` setState(undefined) ` .
27
27
28
28
``` typescript
29
+ type SetState <S > = S | ((prevState ? : S ) => S );
29
30
interface Options <T > {
30
31
defaultValue? : T | (() => T );
31
32
serializer? : (value : T ) => string ;
32
33
deserializer? : (value : string ) => T ;
34
+ onError? : (error : unknown ) => void ;
33
35
}
34
36
35
37
const [state, setState] = useLocalStorageState <T >(
36
38
key : string ,
37
39
options : Options < T >
38
- ): [T ? , (value ? : T | (( previousState : T ) => T ) ) => void ];
40
+ ): [T ? , (value ? : SetState < T > ) => void ];
39
41
` ` `
40
42
43
+ ### Result
44
+
45
+ | Property | Description | Type |
46
+ | -------- | --------------------------- | ------------------------------- |
47
+ | state | Local ` localStorage ` value | ` T ` |
48
+ | setState | Update ` localStorage ` value | ` (value ? : SetState <T >) => void ` |
49
+
41
50
### Options
42
51
43
52
| Property | Description | Type | Default |
Original file line number Diff line number Diff line change 26
26
如果想从 localStorage 中删除这条数据,可以使用 ` setState() ` 或 ` setState(undefined) ` 。
27
27
28
28
``` typescript
29
+ type SetState <S > = S | ((prevState ? : S ) => S );
29
30
interface Options <T > {
30
31
defaultValue? : T | (() => T );
31
32
serializer? : (value : T ) => string ;
@@ -35,9 +36,16 @@ interface Options<T> {
35
36
const [state, setState] = useLocalStorageState <T >(
36
37
key : string ,
37
38
options : Options < T >
38
- ): [T ? , (value ? : T | (( previousState : T ) => T ) ) => void ];
39
+ ): [T ? , (value ? : SetState < T > ) => void ];
39
40
` ` `
40
41
42
+ ### Result
43
+
44
+ | 参数 | 说明 | 类型 |
45
+ | -------- | ---------------------- | ------------------------------- |
46
+ | state | 本地 ` localStorage ` 值 | ` T ` |
47
+ | setState | 设置 ` localStorage ` 值 | ` (value ? : SetState <T >) => void ` |
48
+
41
49
### Options
42
50
43
51
| 参数 | 说明 | 类型 | 默认值 |
You can’t perform that action at this time.
0 commit comments