@exabytellc/app
Version:
EB react app to make everything a little easier!
40 lines • 1.02 kB
JavaScript
import React, { Component } from "react";
import PropTypes from "prop-types";
import { jsx as _jsx } from "react/jsx-runtime";
export default class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = {
hasError: false
};
}
static getDerivedStateFromError() {
return {
hasError: true
};
}
componentDidCatch(error, errorInfo) {
console.error("Error in ErrorBoundary:", error, errorInfo);
this.setState({
hasError: true
}); // Ensure the error state is updated
}
resetError = () => {
this.setState({
hasError: false
});
// window.location.reload(); // Reload the page when the error is reset
};
render() {
if (this.state.hasError) {
return this.props.fallback ?? /*#__PURE__*/_jsx("div", {
children: "Something went wrong. Please try again."
});
}
return this.props.children;
}
}
ErrorBoundary.propTypes = {
children: PropTypes.node.isRequired,
fallback: PropTypes.node
};