# @zimi/hooks

> hooks

Latest version **0.18.6** (published 2026-01-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @zimi/hooks
pnpm add @zimi/hooks
yarn add @zimi/hooks
bun add @zimi/hooks
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.18.6 |
| Published | 2026-01-11 |
| First published | 2022-11-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 50.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | xiaomingTang |
| Maintainers | xiaomingtang |

## Links

- npm: https://www.npmjs.com/package/@zimi/hooks
- Repository: https://github.com/xiaomingTang/xiaoming.git#master
- Homepage: https://github.com/xiaomingTang/xiaoming/tree/master#readme
- Issues: https://github.com/xiaomingTang/xiaoming/issues
- npm.io page: https://npm.io/package/@zimi/hooks

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21

## Recent versions

- 0.18.6 (latest) — 2026-01-11
- 0.18.5 — 2025-07-06
- 0.18.4 — 2025-01-14
- 0.18.3 — 2025-01-09
- 0.18.2 — 2024-11-07
- 0.18.1 — 2024-11-07
- 0.18.0 — 2024-10-25
- 0.17.1 — 2024-03-20
- 0.16.0 — 2023-12-26
- 0.15.0 — 2023-05-15
- 0.14.0 — 2023-05-15
- 0.12.0 — 2023-03-11
- 0.11.0 — 2023-03-11
- 0.10.0 — 2023-03-11
- 0.8.1 — 2023-03-11
- … 12 more at https://npm.io/package/@zimi/hooks/versions

## README

# @zimi/hooks

### install
```
yarn add  @zimi/hooks
```

### examples

[useCombinedRefs](#useCombinedRefs)    
[useElementRect](#useElementRect)    
[useListen](#useListen)    
[useLoading](#useLoading)    
[useRafLoop](#useRafLoop)    
[useSimulateClick](#useSimulateClick)    
[useWarnBeforeUnload](#useWarnBeforeUnload)    
[useWindowSize](#useWindowSize)    

---

#### useCombinedRefs

see [https://itnext.io/reusing-the-ref-from-forwardref-with-react-hooks-4ce9df693dd](https://itnext.io/reusing-the-ref-from-forwardref-with-react-hooks-4ce9df693dd)

``` tsx

const MeaturedInput = forwardRef<HTMLInputElement>((props, ref) => {
  const innerRef = useCombinedRefs(ref)

  useEffect(() => {
    console.log(innerRef.current?.getBoundingClientRect())
  }, [innerRef])

  return <input ref={innerRef} />
})

```
[↑ all examples ↑](#examples)

#### useElementRect
``` ts

function Test() {
  const ref = useRef<HTMLElement>(null)
  const rect = useElementRect(ref)

  return <div ref={ref}>
    hello world
  </div>
}

// or

// out of component
const element = document.querySelector('xxx')

function Test() {
  const rect = useElementRect(element)
}

```
[↑ all examples ↑](#examples)

#### useListen
``` ts

function Test() {
  const [count, setCount] = useState(0)

  // will be triggered when count changed
  useListen(count, (prev, next) => {
    console.log(prev, next)
  })

  // custom equality function
  useListen(count, shallowEqual, (prev, next) => {
    console.log(prev, next)
  })
}

```
[↑ all examples ↑](#examples)

#### useLoading
``` ts

function Test() {
  const [loading, withLoading] = useLoading()

  const { data } = useSWR('xxx', withLoading(asyncTask1))

  return <>
    <Spin spinning={loading} />

    <Button
      onClick={withLoading(asyncTask2)}
    >
      async task
    </Button>
  </>
}


```
[↑ all examples ↑](#examples)

#### useRafLoop

``` tsx

import { useRafLoop } from '@zimi/hooks'

function Test() {
  const now = useRafLoop(() => Date.now())

  return <>{now}</>
}

// or

function Test() {
  const [visibility, setVisibility] = useState(true)

  useRafLoop(() => {
    console.log('rendering...')
  }, {
    enable: visibility,
  })

  return <>
    <p>visibility: {visibility}</p>
    <button onClick={() => {
      setVisibility((prev) => !prev)
    }}>
      toggle visibility
    </button>
  </>
}

```
[↑ all examples ↑](#examples)

#### useSimulateClick

``` ts

function Test() {
  const setClickElem = useSimulateClick({
    onClick: () => {
      console.log('strictly clicked.')
    },
  })

  return (
    <Button ref={setClickElem}>
      Click Me
    </Button>
  )
}

```
[↑ all examples ↑](#examples)

#### useWarnBeforeUnload

``` ts

function Test() {
  // show a confirmation dialog before page unload (beforeunload event, NOT component unload)
  // https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
  useWarnBeforeUnload()
}

```
[↑ all examples ↑](#examples)

#### useWindowSize
``` tsx

function Test() {
  const innerSize = useWindowSize('inner')
  const outerSize = useWindowSize('outer')
}

```
[↑ all examples ↑](#examples)

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