Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/inshadowin/use-request
https://github.com/inshadowin/use-request
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/inshadowin/use-request
- Owner: Inshadowin
- License: mit
- Created: 2024-04-17T14:09:41.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-05-19T13:22:39.000Z (8 months ago)
- Last Synced: 2024-05-19T13:59:06.760Z (8 months ago)
- Language: JavaScript
- Size: 1.03 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# use-react-api-request
Package that allows to easily process simple requests
## Setup
```shell
npm i use-react-api-request
``````tsx
import axios from 'axios';
import { setupGlobals } from 'use-react-api-request';setupGlobals({
// or any other API-client that you use
isCancel: axios.isCancel,
// you can throw errors here
// I use it to handle { data: {}, error: string } results, etc.
resultMiddleware: result => console.log(result),
});
``````tsx
import { useRequest } from 'use-react-api-request';const Component = () => {
const [handleRequest, loading] = useRequest(
(signal: AbortSignal, addition: string) =>
callApi(signal, { params: 'test', addition }),
{
onError: () => {},
onFinally: () => {},
onSuccess: () => {},
resultMiddleware: (result: object) => {
}
deps: ['will cancel out if this changes'],
}
);const onClick = () => handleRequest('Addition');
};
```## Cancelable Promise
If your request is cancelable Promise - return it with 'cancel' function attached