10.09.0 Removing Old Interceptors
withErrorHandler.js
withErrorHandler.js
import React, { Component, Fragment } from "react";
import Modal from "../../components/UI/Modal/Modal";
const withErrorHandler = (WrapperComponent, axios) => {
return class extends Component {
state = {
error: null,
};
componentWillMount() {
this.reqInterceptor = axios.interceptors.request.use((req) => {
this.setState({ error: null });
return req;
});
this.resInterceptor = axios.interceptors.response.use(
(res) => res,
(error) => {
this.setState({ error: error });
}
);
}
componentWillUnmount() {
axios.interceptors.request.eject(this.reqInterceptor);
axios.interceptors.response.eject(this.resInterceptor);
}
errorConfirmedHandler = () => {
this.setState({ error: null });
};
render() {
return (
<Fragment>
<Modal
show={this.state.error}
modalClosed={this.errorConfirmedHandler}
>
{this.state.error ? this.state.error.message : null}
</Modal>
<WrapperComponent {...this.props} />;
</Fragment>
);
}
};
};
export default withErrorHandler;
Check
To check if that works add this temporarily code:
to App.js
App.js
import React, { Component } from "react";
import Layout from "./components/Layout/Layout";
import BurgerBuilder from "./containers/BurgerBuilder/BurgerBuilder";
class App extends Component {
state = {
show: true,
};
componentDidMount() {
setTimeout(() => {
this.setState({ show: false });
}, 5000);
}
render() {
return (
<div>
<Layout>{this.state.show ? <BurgerBuilder /> : null}</Layout>
</div>
);
}
}
export default App;
to withErrorHandler.js
withErrorHandler.js
import React, { Component, Fragment } from "react";
import Modal from "../../components/UI/Modal/Modal";
const withErrorHandler = (WrapperComponent, axios) => {
return class extends Component {
state = {
error: null,
};
componentWillMount() {
this.reqInterceptor = axios.interceptors.request.use((req) => {
this.setState({ error: null });
return req;
});
this.resInterceptor = axios.interceptors.response.use(
(res) => res,
(error) => {
this.setState({ error: error });
}
);
}
componentWillUnmount() {
console.log("Will Unmount", this.reqInterceptor, this.resInterceptor);
axios.interceptors.request.eject(this.reqInterceptor);
axios.interceptors.response.eject(this.resInterceptor);
}
...