# use-basin

> A simple React hook for handling Basin AJAX forms.

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

## Install

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

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2022-02-17 |
| First published | 2022-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ryan Hartman |
| Maintainers | rhartman |
| Keywords | react, hook, form, basin |

## Links

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

## 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-17
- 1.0.1 — 2022-02-17
- 1.0.0 — 2022-02-17

## README

# Basin React Hook

A simple React hook for handling Basin AJAX forms.

## Installation

Install the package:

```sh
npm i -D use-basin
```

**Note**: a React version above 16.8.3 must be installed as a peer dependency.

## Usage

Create a form that will submit to Basin using AJAX:

```js
import useBasin from 'use-basin';

const Form = () => {
  const [state, submit] = useBasin('<form-id>');
  return <form onSubmit={submit}>{/*  inputs... */}</form>;
};
```

You can also create your own onSubmit handlers for custom error checking or other functionality:

```js
const onSubmit = (e) => {
  e.preventDefault();
  const data = new FormData(e.target);
  console.log(data.get('name'));
  // Be sure to call the submit function to actually
  // submit the form.
  submit(e);
};
```

You can also use the returned state object for loaders and error handling:

```js
import useBasin from 'use-basin';

const Form = () => {
  const [{ pending, error, submitted }, submit] = useBasin('<form-id>');
  if (pending) return <div>Submitting...</div>;
  if (error) return <div>Something went wrong =(</div>;
  if (submitted) return <div>Thanks for submitting our form!</div>;
  return <form onSubmit={submit}>{/*  inputs... */}</form>;
};
```

## State Variables

The following variables are returned in an object as the first value in the array:

| Name      | Description                                   | Type      |
| --------- | --------------------------------------------- | --------- |
| Submitted | The form has been submitted                   | `boolean` |
| Pending   | A POST request has started but not completed. | `boolean` |
| Error     | The most recent POST request failed.          | `boolean` |

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