# use-state-track-prop

> A react hook for private state of component tracking some props

Latest version **1.3.1** (published 2023-09-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-state-track-prop
pnpm add use-state-track-prop
yarn add use-state-track-prop
bun add use-state-track-prop
```

## 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.3.1 |
| Published | 2023-09-26 |
| First published | 2019-10-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | 2631541504@qq.com |
| Maintainers | livelybone |
| Keywords | react, hook, state, track, prop, props |

## Links

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

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.3.1 (latest) — 2023-09-26
- 1.3.0 — 2020-03-30
- 1.2.0 — 2019-11-05
- 1.1.7 — 2019-11-04
- 1.1.6 — 2019-11-03
- 1.1.5 — 2019-11-03
- 1.1.4 — 2019-11-02
- 1.1.3 — 2019-11-02
- 1.1.2 — 2019-10-31
- 1.1.1 — 2019-10-30
- 1.1.0 — 2019-10-30
- 1.0.1 — 2019-10-22
- 1.0.0 — 2019-10-22

## README

# use-state-track-prop
[![NPM Version](http://img.shields.io/npm/v/use-state-track-prop.svg?style=flat-square)](https://www.npmjs.com/package/use-state-track-prop)
[![Download Month](http://img.shields.io/npm/dm/use-state-track-prop.svg?style=flat-square)](https://www.npmjs.com/package/use-state-track-prop)
![gzip with dependencies: 1kb](https://img.shields.io/badge/gzip--with--dependencies-1kb-brightgreen.svg "gzip with dependencies: 1kb")
![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 for private state of component tracking some props

## repository
https://github.com/livelybone/use-state-track-prop.git

## Demo
https://github.com/livelybone/use-state-track-prop#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-state-track-prop.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-state-track-prop
```

## Global name - The variable the module exported in `umd` bundle
`useStateTrackProp`

## Interface
```typescript
import { Dispatch, SetStateAction } from 'react'

export interface TReducer<T, P> {
  (props: P, preState?: T): T;
}

/**
 * @param props       Assembly of the prop of the component
 *                    If props is a reference type, use `useMemo` hook to generate it for performance
 *
 * @param reducer     Please make sure the reducer is a pure function
 *
 * @return [state, setState]
 * */
export default function useStateTrackProp<T extends any, P extends any>(props: P, reducer?: TReducer<T, P>): [T, Dispatch<SetStateAction<T>>];
```

## Usage
```typescript jsx
import React from 'react'
import useStateTrackProp from 'use-state-track-prop'

const Comp = ({ value }) => {
  const [$value, setValue] = useStateTrackProp(
    value,
    (value, preValue) => preValue || value,
  )
  
  return (
    <div>
      {$value}
      <button onClick={() => setValue(pre => !pre)}>Toggle Value</button>
    </div>
  )
}
```

Use in html, see what your can use in [CDN: unpkg](https://unpkg.com/use-state-track-prop/lib/umd/)
```html
<-- use what you want -->
<script src="https://unpkg.com/use-state-track-prop/lib/umd/<--module-->.js"></script>
```

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