use-apios
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

use-apios

npm version install size Build Status

Custom React hook for axios.

Install

# npm
$ npm install use-apios axios

# or yarn
$ yarn add use-apios axios

Example

import * as React from 'react';
import axios from 'axios';
import useApios from 'useApios'

type GetRes = {
  name: string
}

const App: React.FC = () => {
  const [{ loading, data, error }, fetch] = useApios(
    () => http.get<GetRes>('/api/user')
  );

  const onClick = React.useCallback(() => {
    fetch();
  }, []);

  return (
    <div>
      <p>User</p>
      {loading && <p>loading</p>}
      {error && <p>error: {error.response.status}</p>}
      {data && (
        <>
          <p>{data.name}</p>
        </>
      )}
      <button onClick={onClick}>GET</button>
    </div>
  );
};

API

  • useApios(fn)

args:

fn: Function

AxiosResponse-Promise returning function.

useApios(() => axios.get('/api'));

return: [{...}, fn]

const [{ loading, error, data, status, statusText, headers, config }, fetch] = useApios(fn)

loading: boolean

default: false.

error: AxiosError

axios's AxiosError.

data, status, statusText, headers, config, request

axios's response data.

https://github.com/axios/axios#response-schema

fetch

The function to exec axios request.

Develop

# build use-apios
$ yarn build

# development: localost:3000
$ yarn dev

Readme

Keywords

Package Sidebar

Install

npm i use-apios

Weekly Downloads

2

Version

0.0.1

License

MIT

Unpacked Size

4.59 kB

Total Files

4

Last publish

Collaborators

  • shooontan