forked from alibaba/hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
124 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useState } from 'react'; | ||
import useLocalStorageState from '../index'; | ||
|
||
describe('useLocalStorageState', () => { | ||
it('should be defined', () => { | ||
expect(useLocalStorageState).toBeDefined(); | ||
}); | ||
|
||
const setUp = (): any => | ||
renderHook(() => { | ||
const [state, setState] = useLocalStorageState('test-key', 'A'); | ||
return { | ||
state, | ||
setState, | ||
}; | ||
}); | ||
|
||
it('getKey should work', () => { | ||
const hook = setUp(); | ||
expect(hook.result.current.state).toEqual('A'); | ||
act(() => { | ||
hook.result.current.setState('B'); | ||
}); | ||
expect(hook.result.current.state).toEqual('B'); | ||
const anotherHook = setUp(); | ||
expect(anotherHook.result.current.state).toEqual('B'); | ||
act(() => { | ||
anotherHook.result.current.setState('C'); | ||
}); | ||
expect(anotherHook.result.current.state).toEqual('C'); | ||
expect(hook.result.current.state).toEqual('B'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
name: useLocalStorageState | ||
route: /useLocalStorageState | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
||
import { Playground } from 'docz'; | ||
import { useRef } from 'react'; | ||
import { Button, Input } from 'antd'; | ||
import useLocalStorageState from './index'; | ||
|
||
# useLocalStorageState | ||
|
||
一个可以将状态持久化存储在 localStorage 中的 Hook 。 | ||
|
||
它的API和 `useState` 非常类似,但是多了一个参数 `key` ,用来指定在 localStorage 中存储时所使用的 `key` 。而它的返回值类型和 `useState` 保持来一致,当调用 `setState` 时,它会自动将新值写入到 localStorage 中。 | ||
|
||
如果想从 localStorage 中删除这条数据,可以使用 `setState()` 或 `setState(undefined)` 。 | ||
|
||
## 代码演示 | ||
|
||
<Playground> | ||
{ | ||
()=>{ | ||
function Demo() { | ||
const [message, setMessage] = useLocalStorageState('user-message', 'Hello~') | ||
return ( | ||
<> | ||
<p>刷新页面后,可以看到输入框中的内容被从 localStorage 中恢复了。</p> | ||
<Input | ||
value={message} | ||
onChange={(e) => {setMessage(e.target.value)}} | ||
placeholder='请输入文字' | ||
style={{width: 200, marginRight: 16}} | ||
/> | ||
<Button onClick={() => {setMessage()}}>重置</Button> | ||
</> | ||
) | ||
} | ||
|
||
return <Demo /> | ||
} | ||
} | ||
</Playground> | ||
|
||
## API | ||
|
||
```typescript | ||
function useLocalStorageState<T extends string = string>( | ||
key: string, | ||
defaultValue?: T, | ||
): [T?, (value?: T) => void] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useState } from 'react'; | ||
|
||
export default function useLocalStorageState<T extends string = string>( | ||
key: string, | ||
defaultValue?: T, | ||
) { | ||
const [state, setState] = useState(() => (localStorage.getItem(key) as T) || defaultValue); | ||
function updateState(value?: T) { | ||
if (value === undefined) { | ||
localStorage.removeItem(key); | ||
setState(defaultValue); | ||
} else { | ||
localStorage.setItem(key, value as string); | ||
setState(value); | ||
} | ||
} | ||
return [state, updateState] as [typeof state, typeof updateState]; | ||
} |