diff --git a/package.json b/package.json index 3c00462d..95de0704 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "dependencies": { "react": "^18.2.0", "react-dom": "^18.2.0", + "react-error-boundary": "^5.0.0", "react-router-dom": "^6.18.0", "recoil": "^0.7.7", "styled-components": "^6.1.1", diff --git a/src/ErrorBoundary.tsx b/src/ErrorBoundary.tsx index 6f921c1d..5568d6df 100644 --- a/src/ErrorBoundary.tsx +++ b/src/ErrorBoundary.tsx @@ -1,43 +1,67 @@ -import type { ErrorInfo, ReactNode } from 'react' -import React, { Component } from 'react' -import { styled } from 'styled-components' +import React, { useState } from 'react'; +import { ErrorBoundary as ReactErrorBoundary } from 'react-error-boundary'; +import { styled } from 'styled-components'; interface Props { - children?: ReactNode + children?: React.ReactNode; } interface State { - error: Error | null - info: ErrorInfo | null -} -class ErrorBoundary extends Component { - state = { - error: null, - info: null, - } - - componentDidCatch(error: Error, info: ErrorInfo): void { - this.setState({ error, info }) - } - - render(): ReactNode { - const { error } = this.state - if (error) { - return - } - return this.props.children - } + error: Error | null; + info: { componentStack: string } | null; } -export default ErrorBoundary +// Fallback UI Component +const ErrorBoundaryFallbackComponent: React.FC = ({ error, info }) => ( + + +

Something went wrong:

+
{error?.message}
+
{info?.componentStack || 'No component stack available.'}
+ + 😞 + +
+
+); + +// Error Boundary Component using react-error-boundary +const ErrorBoundary: React.FC = ({ children }) => { + const [errorInfo, setErrorInfo] = useState(null); + + const handleError = (error: Error, info: React.ErrorInfo) => { + console.error('Logging error:', error); + console.error('Error info:', info); + setErrorInfo(info); + }; + return ( + ( + + )} + onError={handleError} + > + {children} + + ); +}; + +export default ErrorBoundary; + +// Styled components const Layout = styled.div` width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; -` +`; const Message = styled.div` padding: 40px; @@ -45,15 +69,4 @@ const Message = styled.div` border-radius: 5px; font-size: 24px; color: #78909c; -` - -const ErrorBoundaryFallbackComponent = () => ( - - - Something Error Ooccurring - - 😞 - - - -) +`;