diff --git a/src/components/ErrorBoundary/ErrorBoundary.tsx b/src/components/ErrorBoundary/ErrorBoundary.tsx new file mode 100644 index 0000000..9951d29 --- /dev/null +++ b/src/components/ErrorBoundary/ErrorBoundary.tsx @@ -0,0 +1,37 @@ +/* eslint-disable no-console */ +import { Component } from 'react'; + +type MyProps = { + fallback: JSX.Element; + children: JSX.Element; +}; + +type MyState = { + hasError: boolean; +}; + +class ErrorBoundary extends Component { + constructor(props: MyProps) { + super(props); + this.state = { hasError: false }; + } + + static getDerivedStateFromError() { + return { hasError: true }; + } + + componentDidCatch(error: unknown, info: React.ErrorInfo) { + console.log(error, info.componentStack); + } + + render() { + const { hasError } = this.state; + const { fallback, children } = this.props; + if (hasError) { + return fallback; + } + return children; + } +} + +export default ErrorBoundary; diff --git a/src/components/ErrorFallback/ErrorFallback.tsx b/src/components/ErrorFallback/ErrorFallback.tsx new file mode 100644 index 0000000..3b5b32a --- /dev/null +++ b/src/components/ErrorFallback/ErrorFallback.tsx @@ -0,0 +1,13 @@ +const ErrorFallback = () => { + return ( +
+
+ Somehow something managed to crash our app... +
+
We suggest you to reload the page to continue using the app. +
+
+ ); +}; + +export default ErrorFallback; diff --git a/src/main.tsx b/src/main.tsx index a279fd0..a676c58 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -4,6 +4,8 @@ import ReactDOM from 'react-dom/client'; import App from '@/app/App'; +import ErrorBoundary from './components/ErrorBoundary/ErrorBoundary'; +import ErrorFallback from './components/ErrorFallback/ErrorFallback'; import initFirebaseApp from './firebase'; import '@/styles/index.css'; @@ -14,6 +16,8 @@ initFirebaseApp(); ReactDOM.createRoot(document.getElementById('root')!).render( - + }> + + , ); diff --git a/src/router/router.tsx b/src/router/router.tsx index 41269ba..13e141b 100644 --- a/src/router/router.tsx +++ b/src/router/router.tsx @@ -1,5 +1,6 @@ import { createHashRouter } from 'react-router-dom'; +import ErrorFallback from '@/components/ErrorFallback/ErrorFallback'; import MainLayout from '@/layouts/MainLayout'; import LoginPage from '@/pages/LoginPage'; import SettignsPage from '@/pages/SettingsPage'; @@ -14,6 +15,7 @@ export const routes = [ { path: '/', element: , + errorElement: , children: [ { path: ROUTES.WELCOME_PAGE,