Skip to content

olivier1208/use-axios-loader

Repository files navigation

use-axios-loader

use-axios-loader is a tiny React Hook, designed to easily add a loader to all your axios instances.

NPM JavaScript Style Guide

⚙️ Installation

npm install --save use-axios-loader

or

yarn add use-axios-loader

🚀 Usage

import React, { Component } from 'react'
import axiosInstance from 'axios'

import { useAxiosLoader } from 'use-axios-loader'

const MyComponent = () => {
  // Pass the axios instance to the hook
  // Allows you tu customize easily your instance
  const [loading] = useAxiosLoader(axiosInstance)
  return (
    <>
      {loading
      ? <img src="path/to/loader"/>
      : <div>My data</div>
      }
    </>
  )
}

👉 You may also pass an array of URLs to ignore. This is the second parameter accepted by this hook.

const ignoredUrls = ['https://myignoredurl.com', 'anotherignored.co']
const [loading] = useAxiosLoader(axiosInstance, ignoredUrls)

All urls passed to ignoredUrls variable, won't trigger the loader.


License

MIT © olivier1208