axios-handler

This hook works with the axios library, making queries much easier and more convenient.


Keywords
axios-handler, axios, useFetching, fetch, use, hook, hooks, React, React Native
License
ISC
Install
npm install axios-handler@1.0.7

Documentation

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