Skip to content

Commit

Permalink
feat: add useResetState
Browse files Browse the repository at this point in the history
  • Loading branch information
AmbitionsXXXV committed Nov 21, 2023
1 parent 81c8a39 commit e6a509d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const menus = [
{ title: 'useBoolean', link: '/hooks/use-boolean' },
{ title: 'useCounter', link: '/hooks/use-counter' },
{ title: 'useDebounce', link: '/hooks/use-debounce' },
{ title: 'useResetState', link: '/hooks/use-reset-state' },
{ title: 'useSet', link: '/hooks/use-set' },
{ title: 'useToggle', link: '/hooks/use-toggle' },
{ title: 'useUpdate', link: '/hooks/use-update' },
Expand Down
2 changes: 2 additions & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useMemoizedFn from './useMemoizedFn'
import useMount from './useMount'
import useOs from './useOs'
import useRequest from './useRequest'
import useResetState from './useResetState'
import useSet from './useSet'
import useTitle from './useTitle'
import useToggle from './useToggle'
Expand All @@ -36,6 +37,7 @@ export {
useMount,
useOs,
useRequest,
useResetState,
useSet,
useTitle,
useToggle,
Expand Down
31 changes: 31 additions & 0 deletions packages/hooks/src/useResetState/example/example.tsx
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>
)
}
20 changes: 20 additions & 0 deletions packages/hooks/src/useResetState/index.md
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]
```
19 changes: 19 additions & 0 deletions packages/hooks/src/useResetState/index.ts
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

0 comments on commit e6a509d

Please sign in to comment.