# use-watch

> A react hook that use to calling the callback when a part of dependencies changed

Latest version **1.1.1** (published 2021-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-watch
pnpm add use-watch
yarn add use-watch
bun add use-watch
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2021-10-10 |
| First published | 2019-10-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | 2631541504@qq.com |
| Maintainers | livelybone |
| Keywords | react, hook, watch, part of dependencies, call, callback |

## Links

- npm: https://www.npmjs.com/package/use-watch
- Repository: https://github.com/livelybone/use-watch
- Homepage: https://github.com/livelybone/use-watch#readme
- Issues: https://github.com/livelybone/use-watch/issues
- npm.io page: https://npm.io/package/use-watch

## Recent versions

- 1.1.1 (latest) — 2021-10-10
- 1.1.0 — 2021-09-20
- 1.0.1 — 2019-12-02
- 1.0.0 — 2019-10-31

## README

# use-watch
[![NPM Version](http://img.shields.io/npm/v/use-watch.svg?style=flat-square)](https://www.npmjs.com/package/use-watch)
[![Download Month](http://img.shields.io/npm/dm/use-watch.svg?style=flat-square)](https://www.npmjs.com/package/use-watch)
![gzip with dependencies: 0.5kb](https://img.shields.io/badge/gzip--with--dependencies-0.5kb-brightgreen.svg "gzip with dependencies: 0.5kb")
![typescript](https://img.shields.io/badge/typescript-supported-blue.svg "typescript")
![pkg.module](https://img.shields.io/badge/pkg.module-supported-blue.svg "pkg.module")

> `pkg.module supported`, which means that you can apply tree-shaking in you project

[中文文档](./README-CN.md)

A react hook that use to calling the callback when a part of dependencies changed

## repository
https://github.com/livelybone/use-watch.git

## Demo
https://github.com/livelybone/use-watch#readme

## Run Example
Your can see the usage by run the example of the module, here is the step:

1. Clone the library `git clone https://github.com/livelybone/use-watch.git`
2. Go to the directory `cd your-module-directory`
3. Install npm dependencies `npm i`(use taobao registry: `npm i --registry=http://registry.npm.taobao.org`)
4. Open service `npm run dev`
5. See the example(usually is `http://127.0.0.1/examples/test.html`) in your browser

## Installation
```bash
npm i -S use-watch
```

## Global name - The variable the module exported in `umd` bundle
`useWatch`

## Interface
See what method or params you can use in [index.d.ts](./index.d.ts)

## Usage
```typescript jsx
import React, { useState, useMemo } from 'react'
import useWatch from 'use-watch'

const Comp = () => {
  const [count, setCount] = useState(0)
  const [count1, setCount1] = useState(0)
  const [count2, setCount2] = useState(0)

  useWatch(count, (val) => console.log('---------- \ncount change to: ', val), { immediate: true })
  useWatch(count1, (val) => console.log('---------- \ncount1 change to: ', val))
  useWatch(count2, (val) => console.log('---------- \ncount2 change to: ', val))
  useWatch(useMemo(() => ({ count, count1 }), [count, count1]), (val) => {
    console.log('---------- \ncount or count1 change to: ' , val)
    console.log('now the count2 is: ', count2)
  })

  console.log('render')
  return (
    <div>
      <button onClick={() => setCount(pre => pre + 1)}>add count</button>
      <button onClick={() => setCount1(pre => pre + 1)}>add count1</button>
      <button onClick={() => setCount2(pre => pre + 1)}>add count2</button>
    </div>
  )
}
```

Use in html, see what your can use in [CDN: unpkg](https://unpkg.com/use-watch/lib/umd/)
```html
<script src="https://unpkg.com/use-watch/lib/umd/index.js"></script>
```

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