npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-axios-hook

v2.0.2

Published

axios hook for react

Downloads

10

Readme

react-axios-hook

Build Status npm version bundlephobia

:fire: A React Hook based on axios.

Features

  • Using Axios with React Hook
  • Support Typescript
  • Support cancel request
  • Global Config with AxiosConfig component
  • Flexible with config axios or axios instance
  • FullControl with axios behavior,support loading status, manual refresh, fetching control...

Installation

npm install axios react-axios-hook

OR

yarn add axios react-axios-hook

axios is a peer dependency and needs to be installed explicitly

Quick Start

Basic Usage

import useAxios from 'react-axios-hook'

function App() {
  const [{ response, loading, error }, refresh] = useAxios('https://www.mxnzp.com/api/holiday/single/20181121', [])

  if (error) {
    return <div>{JSON.stringify(error)}</div>
  }
  return loading ? <div>Loading...</div> : (<div>{JSON.stringify(response)}</div>)
}
ReactDOM.render(<App />, document.getElementById('root'))

Global Config

import useAxios, { AxiosConfig } from 'react-axios-hook'

function Demo() {
  const [{ response, loading, error }, refresh] = useAxios('https://www.mxnzp.com/api/holiday/single/20181121')

  if (error) {
    return <div>{JSON.stringify(error)}</div>
  }
  return loading ? <div>Loading...</div> : (<div>{JSON.stringify(response)}</div>)
}
function App() {
  return (
    <AxiosConfig config={{baseURL: 'https://www.mxnzp.com/api/'}}>
      <Demo />
    </AxiosConfig>
  )
}
ReactDOM.render(<App />, document.getElementById('root'))

Documentation

API

API

The package exports one default export and a name export AxiosConfig:

import useAxios, { AxiosConfig } from 'react-axios-hook'

useAxios(url|config, options?, dependencies?)

The main React hook to execute HTTP requests.

  • url|config - The request URL or config object, the same argument accepted by axios.
  • options - An options object.
    • trigger ( true ) - If false, the request is not executed immediately. Useful for non-GET requests that should not be executed when the component renders.
    • clear(false) - If true, new request will clear old response when request start
    • cancelable ( false ) - If true, the last request will be canceled if last request is not finished when new request get into processing.
    • initResponse - If set, response will use this value as initial value
  • dependencies - dependencies array

Returns:

[{ loading, error, response }, refresh]

  • loading - True if the request is in progress, otherwise False.

  • error - The error value

  • response - The whole success response object.

  • isCacel - True if the request is canceled, otherwise False.

  • refresh([url|config]) - A function to execute the request manually, bypassing the cache by default.

    • url|config - Same with useAxios's first parameter, which is shallow-merged with the config object provided when invoking the hook.

AxiosConfig

react-axios-hook uses a default axios object unless you define your own by AxiosConfig component.

This component will define a global axiosInstance for further use.

Attension: This is a React Component

Props

  • instance - the custom axios instance your define
  • config - config object, the same argument accepted by axios, used to create a axios instance
  • options - global options
    • trigger(true)
    • cancelable(false)
    • clear(false)

When defining both instance and config, it will use instance and ignore config parameter.

Credits

react-axios-hook is inspired by axios-hooks and swr

License

MIT