use-react-api-request
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

use-react-api-request

Package that allows to easily process simple requests

Setup

npm i use-react-api-request
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),
});
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

/use-react-api-request/

    Package Sidebar

    Install

    npm i use-react-api-request

    Weekly Downloads

    112

    Version

    1.2.1

    License

    MIT

    Unpacked Size

    13.4 kB

    Total Files

    15

    Last publish

    Collaborators

    • kar.antony