# use-partial-state

> React's useState hook with easier partial updates

Latest version **1.0.2** (published 2021-06-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-partial-state
pnpm add use-partial-state
yarn add use-partial-state
bun add use-partial-state
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-06-02 |
| First published | 2020-07-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Renat Zamaletdinov |
| Maintainers | zamaletto |
| Keywords | react, hook, hooks, react-hooks, useState, state, update, partial, typescript |

## Links

- npm: https://www.npmjs.com/package/use-partial-state
- Repository: https://github.com/Zamaletdinov/use-partial-state
- Issues: https://github.com/Zamaletdinov/use-partial-state/issues
- npm.io page: https://npm.io/package/use-partial-state

## 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.0.2 (latest) — 2021-06-02
- 1.0.1 — 2021-02-20
- 1.0.0 — 2020-07-19

## README

[![View on NPM](http://img.shields.io/npm/v/use-partial-state.svg)](https://www.npmjs.com/package/use-partial-state)
[![Build Status](https://travis-ci.com/Zamaletdinov/use-partial-state.svg?branch=master)](https://travis-ci.com/Zamaletdinov/use-partial-state)
[![Dependencies Status](https://david-dm.org/Zamaletdinov/use-partial-state/status.svg)](https://david-dm.org/Zamaletdinov/use-partial-state)

# use-partial-state

This library was built on top of React's useState hook that allows to ease partial state updates

## Usage

```ts
import { usePartialState } from 'use-partial-state';

type ComplexObject = { index: number; value: string };
const initialValue: ComplexObject = { index: 1, value: 'initial value' };
const newValue: ComplexObject = { index: 2, value: 'new value' };
const partialUpdate: Partial<ComplexObject> = { value: 'updated value' };

const [complexObject, setComplexObject, updateComplexObject] = usePartialState<ComplexObject>(initialValue);
console.log(complexObject);
// { index: 1, value: 'initial value' }

setComplexObject(newValue);
console.log(complexObject);
// { index: 2, value: 'new value' }

updateComplexObject(partialUpdate);
console.log(complexObject);
// { index: 2, value: 'updated value' }
```

If the `initialValue` or previous state is undefined then partial update value will be used as final one:

```ts
import { usePartialState } from 'use-partial-state';

type ComplexObject = { index: number; value: string };
const partialUpdate: Partial<ComplexObject> = { value: 'updated value' };

const [complexObject, setComplexObject, updateComplexObject] = usePartialState<ComplexObject>();

updateComplexObject(partialUpdate);
console.log(complexObject);
// { value: 'updated value' }
```

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