I'm using ErrorBoundary in my React app and want to redirect to the main page when an error occurs - but my redirect is not working. Instead, the application stays on the same page.
In comparison, adding a button allows the user to switch pages on clicking it.
But I instead want to automatically take the user there when the error occurs.
Surely there must be an easy way to do this that I'm missing.
import React from 'react';
import { Redirect } from 'react-router-dom';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { error: null, errorInfo: null };
}
componentDidCatch(error, errorInfo) {
this.setState({
error: error,
errorInfo: errorInfo
})
console.log(error, errorInfo);
}
render() {
if (this.state.errorInfo) {
return (
<div>
{/* <Button href="/index.html">Go Home</Button> */}
<Redirect to="/index.html" />
</div>
);
}
return this.props.children;
}
}
export default ErrorBoundary;
I've also tried returning a Route with no luck:
render() {
if (this.state.errorInfo) {
return (
<Route path="/Home" component={Home} />
);
}