react-axios-effect
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

React Axios Effect

GitHub GitHub tag (latest SemVer) TypeScript Support

Simple React Effect Hook that executes callback with axios CancelToken as argument. Request cancel will be performed as effect destruction.

Installation

Using npm

npm i react-axios-effect

Example

useAxiosEffect

Source

Execute API effect with cancel token. Request will be cancelen on component unmount.

import * as React from "react"
import axios from "axios";
import { useAxiosEffect } from "react-axios-effect";

export const FunctionalComponent = () => {
    const [ data, setData ] = React.useState();

    useAxiosEffect(
        /* effect that returns AxiosPromise */
        (cancelToken) => axios
            .get("https://example.com/api/v1/", { cancelToken })
            .then((response) => setData(response.data))
        /* dependencies */
        [ setData ]
    );

    if (data === undefined) {
        /* render placeholder for loading state */
        return <span>Loading...</span>
    }

    /* render your component */
    return <pre>{JSON.strinfigy(data)}</pre>;
};

Contributors

License

MIT

Package Sidebar

Install

npm i react-axios-effect

Weekly Downloads

3

Version

1.0.1

License

UNLICENSED

Unpacked Size

9.74 kB

Total Files

14

Last publish

Collaborators

  • horat1us