# use-prop-change

> React hook for simplifying nested state manipulations

Latest version **0.1.4** (published 2020-06-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-prop-change
pnpm add use-prop-change
yarn add use-prop-change
bun add use-prop-change
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2020-06-18 |
| First published | 2020-06-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 236.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Vladislav Murashchenko |
| Maintainers | murashchenko |
| Keywords | front-end, react, forms, form, state, hooks |

## Links

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

## Dependencies (1)

- [react](https://npm.io/package/react.md) ^16.13.1

## 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

- 0.1.4 (latest) — 2020-06-18
- 0.1.3 — 2020-06-18
- 0.1.2 — 2020-06-18
- 0.1.1 — 2020-06-18
- 0.1.0 — 2020-06-18

## README

# use-prop-change

`use-prop-change` React hook for simplifying `setState` decomposition into smaller setters for particular state parts.
It can usefull when you need:

- transfer control over part of the state to another component;
- set or change some data in nested state tree;
- simplify working with forms without big library;

[example on codesandbox](https://codesandbox.io/s/github/VladislavMurashchenko/use-prop-change-sandbox?file=/src/containers/UserForm.tsx)

## Install

```sh
$ npm install use-prop-change
```

or

```sh
$ yarn add use-prop-change
```

## Usage Example

```javascript
import React, { useState } from "react";
import usePropChange from "use-prop-change";

import Friends from "./Friends"; // this component responsible for friends field

const forInput = (handler) => (e) => handler(e.target.value);

const defaultPerson = {
  name: "Dasha",
  age: "23",
  friends: [
    /* whatever here */
  ],
};

export const PersonForm = () => {
  const [person, setPerson] = useState(defaultPerson);

  const handlePersonProp = usePropChange(setPerson);

  return (
    <form onSubmit={/* do something */}>
      <input
        value={person.name}
        onChange={forInput(handlePersonProp("name"))}
      />
      <input value={person.age} onChange={forInput(handlePersonProp("age"))} />
      <Friends
        friends={person.friends}
        onFriendsChange={handlePersonProp("friends")}
      />
    </form>
  );
};
```

## How it works?

`use-prop-change` use [ramda lens](https://ramdajs.com/docs/#lens) under the hood.
But you don't need understand them to use this hook.

For example above we can replace

```javascript
const handlePersonProp = usePropChange(setPerson);
```

to

```javascript
const handlePersonProp = (name) => (value) => {
  setPerson((prevState) => ({
    ...prevState,
    [name]: typeof value === "function" ? value(prevState[name]) : value,
  }));
};
```

But note, `handlePersonProp` returned from `usePropChange` more powerfull:

- if first argument is number, new state will be an Array
- if first argument is string, new state will be an Object
- if first argument is array, state will be changed on this path
- 2 arguments (name and value) can be passed together, to just make changes once

You can see all this on [example](https://codesandbox.io/s/github/VladislavMurashchenko/use-prop-change-sandbox?file=/src/containers/UserForm.tsx)

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