# @toolkitss/custom-hook

> custom hook setState forceUpdate

Latest version **0.0.7** (published 2020-05-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @toolkitss/custom-hook
pnpm add @toolkitss/custom-hook
yarn add @toolkitss/custom-hook
bun add @toolkitss/custom-hook
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.7 |
| Published | 2020-05-23 |
| First published | 2019-12-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 12 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | sunny |
| Maintainers | sunnyshining |

## Links

- npm: https://www.npmjs.com/package/@toolkitss/custom-hook
- Repository: https://github.com/sunnyShining/toolkitss
- Homepage: https://github.com/sunnyShining/toolkitss/tree/master/packages/custom-hook#readme
- Issues: https://github.com/sunnyShining/toolkitss/issues
- npm.io page: https://npm.io/package/@toolkitss/custom-hook

## Dependencies (1)

- [@toolkitss/helper](https://npm.io/package/@toolkitss/helper.md) ^0.0.17

## Recent versions

- 0.0.7 (latest) — 2020-05-23
- 0.0.6 — 2020-04-19
- 0.0.5 — 2020-04-19
- 0.0.4 — 2020-04-18
- 0.0.3 — 2019-12-30

## README

# @toolkitss/custom-hook

> some useful custom hook

# Installation

## Using npm:

```zsh
$ npm i -g npm
$ npm i --save @toolkitss/custom-hook
```

## Using yarn:

```zsh
$ yarn add @toolkitss/custom-hook
```

## Usage

```tsx
import React, { useRef } from 'react'
import { useSetState, useForceUpdate, useErrorBoundary } from '@toolkitss/custom-hook'
import { Input, Button } from 'antd'

interface IState {
  age: number
  name: string
}

export const SetState: React.FC = () => {
  /** init setState */
  const [state, setState] = useSetState<IState>({
    name: 'sunny',
    age: 18,
  }, () => {
    console.log('init')
  })
  /** 受控组件 */
  const edit = (e: React.ChangeEvent<HTMLInputElement>) => {
    const val = e.target.value
    setState({
      name: val
    }, () => {
      /** 更新结束后回调，可不传 */
      console.log('updated')
    })
  }
  /** 通过函数去更新state */
  const update = () => {
    setState((prevState) => {
      return {
        age: prevState.age + 1
      }
    /** 更新结束后回调 */
    }, () => {
      console.log('updated2222')
    })
  }
  return (
    <div className="setState">
      <Input value={state.name} onChange={edit} />
      <span>{state.name}</span>
      <Button onClick={() => setState({age: state.age + 1})}>add age</Button>
      <span>{state.age}</span>
      <Button onClick={() => setState({
        name: 'sunny1',
        age: 30
      })}>update all</Button>
      <Button onClick={update}>function update</Button>
    </div>
  )
}

export const ForceUpdate: React.FC = () => {
  const number = useRef<number>(18)
  const [, forceUpdate] = useForceUpdate()

  const add = () => {
    number.current = ++number.current
  }
  // 测试强制更新
  const testForceUpdate = () => {
    forceUpdate()
  }
  return (
    <div className="forUpdate">
      <span>{number.current}</span>
      <Button onClick={add}>add</Button>
      <Button onClick={testForceUpdate}>force update</Button>
    </div>
  )
}

// @ts-ignore
const ErrorC = () => <Button>{a}</Button>

export const ErrorBoundaryTest: React.FC = () => {
  const { captureError, ErrorBoundary } = useErrorBoundary()
  console.log(captureError)
  return (
    <div>
      <ErrorBoundary
        renderError={props => {
          console.log('renderError', props)
          return <div>render Error</div>
        }}
      >
        <ErrorC />
      </ErrorBoundary>
    </div>
  )
}

```

---
_Source: https://npm.io/package/@toolkitss/custom-hook · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
