Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/rjhartman/use-basin
- Owner: rjhartman
- Created: 2022-02-17T18:28:36.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-23T19:13:22.000Z (almost 2 years ago)
- Last Synced: 2024-09-17T13:29:19.291Z (4 months ago)
- Language: TypeScript
- Size: 248 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
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) returnSubmitting...;
if (error) returnSomething went wrong =(;
if (submitted) returnThanks 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` |