1.0.2 • Published 1 year ago

axios-query-hook v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Axios hook for React

React hook built to simplify using Axios with Reacts and typescript.

Installation

Yarn

  yarn add axios-query-hook

NPM

  npm i axios-query-hook

Usage

Import AxiosQueryProvider

  import { AxiosQueryProvider } from "axios-query-hook"

Decalare axios configuration. Add your API base url and headers here.

  const config = {
      headers: {
        Authorization: `Bearer ${token}`,
      } as AxiosRequestHeaders,
    baseUrl: "https://dummyjson.com",
  }

Wrap your app with AxiosQueryProvider

  function App() {
    return (
      <AxiosQueryProvider config={config}>
        <div className="App">
        </div>
      </AxiosQueryProvider>
    )
  }

Example usage

You can now use useQuery in all your components.

  import { useQuery } from "axios-query-hook"

useQuery takes generic request and response types which provides expected type for both our POST request data and query response.

    const { requestHandler, response, loading, error } = useQuery<
      RegisterRequestType,
      RegisterResponseType
  >({
      method: "POST",
      url: "auth/login",
      data: {
        username: "kminchelle",
        password: "0lelplR",
      },
  })

useQuery Props

PropertyTypeUse
fetchOnMountBooleanRequest is sent as soon as component mounts when set to true
dataAxios Request body
Method"POST","GET", "PUT" e.t.cSpecifies request method
urlstringRequest route

useQuery returns :

  • requestHandler - Call to initiate request.
  • loading - Indicates request loading state.
  • error - contains request error when it fails.
  • response - contains request response on success.

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago