-
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
1 parent
81c8a39
commit e6a509d
Showing
5 changed files
with
73 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Button } from 'antd' | ||
import { useResetState } from 'etc-hooks' | ||
import React from 'react' | ||
|
||
interface State { | ||
hello: string | ||
count: number | ||
} | ||
|
||
export default () => { | ||
const [state, setState, resetState] = useResetState<State>({ | ||
hello: '', | ||
count: 0, | ||
}) | ||
|
||
return ( | ||
<div> | ||
<pre>{JSON.stringify(state, null, 2)}</pre> | ||
<p> | ||
<Button | ||
style={{ marginRight: '8px' }} | ||
onClick={() => setState({ hello: 'world', count: 1 })} | ||
> | ||
set hello and count | ||
</Button> | ||
|
||
<Button onClick={resetState}>resetState</Button> | ||
</p> | ||
</div> | ||
) | ||
} |
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,20 @@ | ||
--- | ||
nav: | ||
path: /hooks | ||
--- | ||
|
||
# useResetState | ||
|
||
提供重置 state 方法的 Hooks,用法与 `React.useState` 基本一致。 | ||
|
||
## 代码演示 | ||
|
||
<code hideActions='["CSB"]' src="./example/example.tsx"></code> | ||
|
||
## API | ||
|
||
```typescript | ||
const [state, setState, reset] = useResetState<S>( | ||
initialState: S | (() => S), | ||
): [S, Dispatch<SetStateAction<S>>, () => 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,19 @@ | ||
import type { Dispatch, SetStateAction } from 'react' | ||
import { useState } from 'react' | ||
import useMemoizedFn from '../useMemoizedFn' | ||
|
||
type ResetState = () => void | ||
|
||
const useResetState = <S>( | ||
initialState: S | (() => S), | ||
): [S, Dispatch<SetStateAction<S>>, ResetState] => { | ||
const [state, setState] = useState(initialState) | ||
|
||
const reset = useMemoizedFn(() => { | ||
setState(initialState) | ||
}) | ||
|
||
return [state, setState, reset] | ||
} | ||
|
||
export default useResetState |