Last active
November 5, 2022 17:58
-
-
Save eshaan7/c9d55f809a6c480150815e330f6120f1 to your computer and use it in GitHub Desktop.
Use axios interceptors with react-top-loading-bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import LoadingBar from "react-top-loading-bar"; // https://github.com/klendi/react-top-loading-bar | |
import axios from "axios"; | |
export default function TopLoadingBar() { | |
// loading bar component ref | |
const ref = React.useRef(null); | |
React.useEffect(() => { | |
// Add a request interceptor | |
axios.interceptors.request.use( | |
(config) => { | |
if (ref?.current) ref.current.continuousStart(); | |
return config; | |
}, | |
(error) => Promise.reject(error) | |
); | |
// Add a response interceptor | |
axios.interceptors.response.use( | |
(response) => { | |
if (ref?.current) ref.current.complete(); | |
return response; | |
}, | |
(error) => { | |
if (ref?.current) ref.current.complete(); | |
return Promise.reject(error); | |
} | |
); | |
}, []); | |
return <LoadingBar shadow color="#9441b7" ref={ref} />; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesnt seem to work for me.