axios-handler

1.0.7 • Public • Published

axios-handler

axios-handler is a React/React Native hook that makes it easier and more convenient to work with axios library when executing queries.

Installation

Using npm:

npm i axios-handler axios

Using yarn:

yarn add axios-handler axios

Usage

Import useFetching:

import {useFetching} from 'axios-handler';
import {FC, useEffect} from 'react';
import axios from 'axios'

const Component: FC = () => {
  const {fetch, data, loading, error} = useFetching({
    query: async () => await axios.get('YourApi'),
    onComplete: response => {
      console.log('response', response);
    },
    onError: error => {
      console.log('error', error);
    },
    initialLoading: true
  });

  useEffect(() => {
    fetch();
  }, []);

  return null;
};

export default Component;
  • fetch: function to start a request
  • data: to receive data
  • loading: request loading state
  • error: error occurred during the request
  • query: axios query return
  • onComplete: executed after a successful request and returns the result
  • onError: executed in case of a request error and returns the result
  • initialLoading: initial loading state value

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

ISC

Package Sidebar

Install

npm i axios-handler

Weekly Downloads

15

Version

1.0.7

License

ISC

Unpacked Size

4 kB

Total Files

5

Last publish

Collaborators

  • ekosh02