File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -6,3 +6,4 @@ export * from './selection';
6
6
export * from './set' ;
7
7
export * from './timeout' ;
8
8
export * from './pagination' ;
9
+ export * from './local-storage' ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * title: 将 state 持久化在 localStorage 中
3
+ * desc: 刷新页面后,可以看到输入框中的内容被从 localStorage 中恢复了。
4
+ */
5
+
6
+ import type { FunctionComponent } from 'react' ;
7
+ import { Space , Input , Button } from 'antd' ;
8
+ import 'antd/dist/antd.css' ;
9
+ import { useLocalStorage } from '..' ;
10
+
11
+ const Component : FunctionComponent = ( ) => {
12
+ const [ value , setValue ] = useLocalStorage ( 'localStorageDemo' , '' ) ;
13
+
14
+ return (
15
+ < >
16
+ < Space direction = "vertical" >
17
+ < Space >
18
+ < Input value = { value } onChange = { ( e ) => setValue ( e . target . value ) } />
19
+ </ Space >
20
+ < Space >
21
+ < Button type = "primary" onClick = { ( ) => setValue ( '' ) } >
22
+ 重置
23
+ </ Button >
24
+ </ Space >
25
+ </ Space >
26
+ </ >
27
+ ) ;
28
+ } ;
29
+
30
+ export default Component ;
Original file line number Diff line number Diff line change
1
+ ---
2
+ title : useLocalStorage
3
+ nav :
4
+ title : Hooks
5
+ path : /hook
6
+ order : 2
7
+ ---
8
+
9
+ # useLocalStorage
10
+
11
+ 管理 localStorage 的 hook
12
+
13
+ <code src =" ./demo/useLocalStorage.tsx " >
14
+
15
+ # API
16
+
17
+ ``` typescript
18
+ const [
19
+ state,
20
+ setState,
21
+ ] = useLocalStorage <T >(
22
+ key : string ,
23
+ initialValue : T ,
24
+ );
25
+ ```
26
+
27
+ # 返回值
28
+
29
+ | 参数 | 说明 | 类型 |
30
+ | -------- | --------------------------------- | ------------------ |
31
+ | state | 状态值 | boolean |
32
+ | setState | 设置新状态值并同步到 localStorage | (value: T) => void |
You can’t perform that action at this time.
0 commit comments