# useformreducer

> React controlled-form hook via react.useReducer

Latest version **1.0.3** (published 2022-02-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install useformreducer
pnpm add useformreducer
yarn add useformreducer
bun add useformreducer
```

## 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.3 |
| Published | 2022-02-04 |
| First published | 2022-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 4.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Patricio |
| Maintainers | patgarcia |
| Keywords | useformreducer, react |

## Links

- npm: https://www.npmjs.com/package/useformreducer
- Repository: https://github.com/patgarcia/useFormReducer
- Homepage: https://github.com/patgarcia/useFormReducer#readme
- Issues: https://github.com/patgarcia/useFormReducer/issues
- npm.io page: https://npm.io/package/useformreducer

## Dependencies (1)

- [prop-types](https://npm.io/package/prop-types.md) ^15.8.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

- 1.0.3 (latest) — 2022-02-04
- 1.0.2 — 2022-02-04
- 1.0.1 — 2022-02-04
- 1.0.0 — 2022-02-04

## README

# [useFormReducer](https://reactjs.org/) &nbsp; ![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)

`useFormReducer` is a custom hook that provides the ability to have controlled-forms without the hassle. It implements a reducer via react's `useReducer` hook.

It abstracts several patterns involving controlled-forms:

- **handle change:** pass a "state setter" to your form input `onChange` attribute and it updates the state without going through destructuring the event key + value with the state.
- **validation:** validate an email field by default or pass custom validators to provide support for different field-type validation.
- **error state:** pass an error state setter to capture validation errors.

## use

```bash
$ npm install useformreducer
```

## use

Within the component that holds the form:

```jsx
// import the package
import useFormReducer from 'useformreducer';

// set the form state hook
const [form, setForm] = useFormReducer();

// within the form element
<form>
  ...
  <input
    type="email"
    value={form.email}
    name="email"
    onChange={setForm}
    onBlur={setForm}
  />
  ...
</form>;
```

\* Setting `onBlur` provides specific validation when leaving the input field. An example of this logic is available within the included validator for reference. When using a validator, a passing the `errorSetter` argument is recommended.

## More info

- This may be obvioys but just-in-case, you can use the `form` state object to pass it in the body of your request.

## Special thanks:

Shoutout to [@stevekinney](https://github.com/stevekinney) for sharing the cool trick of using reducers to simplify state management in a form, and avoid the use of the `setState` hook. [State Management in Pure React, v2](https://frontendmasters.com/courses/pure-react-state/)

### License

React is [MIT licensed](./LICENSE).

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