Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rjhartman/use-basin

A simple React hook for handling Basin AJAX forms.
https://github.com/rjhartman/use-basin

Last synced: about 2 months ago
JSON representation

A simple React hook for handling Basin AJAX forms.

Awesome Lists containing this project

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('');
return {/* inputs... */};
};
```

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('');
if (pending) return

Submitting...
;
if (error) return
Something went wrong =(
;
if (submitted) return
Thanks for submitting our form!
;
return {/* inputs... */};
};
```

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