Skip to content

Commit fe04cc0

Browse files
author
qiaoyuwen
committed
feat: add useLocalStorage doc
1 parent a3acae6 commit fe04cc0

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

packages/react-hooks/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './selection';
66
export * from './set';
77
export * from './timeout';
88
export * from './pagination';
9+
export * from './local-storage';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 |

0 commit comments

Comments
 (0)