I am use axios for API call in a React project, and I want to add a loading or spinning effect globally in between a api call's request and response in my axios interceptor, here is the code of my interceptor.
import Axios from 'axios'
Axios.interceptors.request.use(function (config) {
// spinning start to show
const token = window.localStorage.token;
if (token) {
config.headers.Authorization = `token ${token}`
}
return config
}, function (error) {
return Promise.reject(error);
});
Axios.interceptors.response.use(function (response) {
// spinning hide
return response;
}, function (error) {
return Promise.reject(error);
});
Perhaps you could take a simpler approach, where you display a full-screen loading message while your app is busy loading data via axios?
For example, you could make the following additions to your code/project to show a global "loading message" on screen while during axio requests:
CSS:
/* Define css class for global loading message to cover
screen during axios operations */
.loading-indicator:before {
content: '';
background: #000000cc;
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 1000;
}
.loading-indicator:after {
content: 'Loading';
position: fixed;
width: 100%;
top: 50%;
left: 0;
z-index: 1001;
color:white;
text-align:center;
font-weight:bold;
font-size:1.5rem;
}
Javascript:
Axios.interceptors.request.use(function (config) {
// spinning start to show
// UPDATE: Add this code to show global loading indicator
document.body.classList.add('loading-indicator');
const token = window.localStorage.token;
if (token) {
config.headers.Authorization = `token ${token}`
}
return config
}, function (error) {
return Promise.reject(error);
});
Axios.interceptors.response.use(function (response) {
// spinning hide
// UPDATE: Add this code to hide global loading indicator
document.body.classList.remove('loading-indicator');
return response;
}, function (error) {
return Promise.reject(error);
});
Using this method, you could even use CSS3 animations to replace the "loading" message with a animated spinner, or something similar - hope this helps!
You can setup axios interceptors in a higher level component let's say App. You can define a loading state in the reducer with "SHOW_LOADER" & "HIDE_LOADER" action types. These interceptors will dispatch the corresponding actions before a request is sent and received via axios, updating the loading state in store through which you can render a Loader component.
Hope this answers your question.
App Component
import React from 'react';
import axios from 'axios'
import { connect } from 'react-redux';
import { loading } from '../actions'
import Loader from './Loader'
class App extends React.Component{
componentWillMount(){
const self = this
axios.interceptors.request.use(function (config) {
// spinning start to show
self.props.loading(true)
return config
}, function (error) {
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
// spinning hide
self.props.loading(false)
return response;
}, function (error) {
return Promise.reject(error);
});
}
render(){
return (
<div>
{ this.props.loader ? <Loader /> : null }
{/*
Your other components
*/}
</div>
)
}
}
const mapStateToProps = (state)=>{
return {
loader: state.loader
}
}
export default connect(mapStateToProps,{
loading
})(App);
Loader Reducer
const loader = (state = false, action) => {
switch (action.type) {
case "SHOW_LOADER":
return action.data;
break;
case "HIDE_LOADER":
return action.data;
break;
default:
return state;
}
}
export default loader;
Action
export const loading = (bool)=>{
return bool ? {
type:"SHOW_LOADER",
data:bool
}: {
type: "HIDE_LOADER",
data: bool
}
}