Skip to content

Instantly share code, notes, and snippets.

@eshaan7
Last active November 5, 2022 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eshaan7/c9d55f809a6c480150815e330f6120f1 to your computer and use it in GitHub Desktop.
Save eshaan7/c9d55f809a6c480150815e330f6120f1 to your computer and use it in GitHub Desktop.
Use axios interceptors with react-top-loading-bar
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} />;
}
@jaycentdrysdale
Copy link

Doesnt seem to work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment