The useAxios hook for React that automatically handles the request cancellation on unmount, triggers the call after your component mount or manually, etc.
npm install --save react-hooks-box
Or with yarn:
yarn add react-hooks-box
import useAxios from 'react-hooks-box/useAxios';
then inside your functional component:
const [{ isLoading, response, error }, request] = useAxios({
method: 'get',
url: 'my-url',
afterMount: true,
axiosConfig
});
Inputs:
- afterMount
{boolean}
: set to true will execute the axios call just after the mount. It's false by default. - axiosConfig
{Object}
: all classic axios config object.
Outputs
- request
{Function}
: a function that will trigger axios call manually with extended axios config object (optional) as parameter. - isLoading
{boolean}
: a flag set to true when the axios call is in process, by default 'false'. - response
{Object}
: an object that contains the data response if the axios call is successful, by default 'undefined'. - error
{Object}
: an object that contains the error object if the axios call has failed, by default 'undefined'.
MIT