-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22bb883
commit 6483f50
Showing
2 changed files
with
72 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react' | ||
|
||
// Based on https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary | ||
const DEFAULT_ERROR = 'Your device doesn’t support rendering this map.' | ||
class ErrorBoundary extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { errorMessage: null } | ||
} | ||
|
||
static getDerivedStateFromError(error) { | ||
// Update state so the next render will show the fallback UI. | ||
return { | ||
errorMessage: error.message ?? defaultError, | ||
} | ||
} | ||
|
||
componentDidCatch(error, info) { | ||
// Example "componentStack": | ||
// in ComponentThatThrows (created by App) | ||
// in ErrorBoundary (created by App) | ||
// in div (created by App) | ||
// in App | ||
console.error(error, info.componentStack) | ||
} | ||
|
||
render() { | ||
if (this.state.errorMessage) { | ||
// You can render any custom fallback UI | ||
return ( | ||
<div style={{ textAlign: 'center', padding: 24 }}> | ||
{this.props.showErrorTrace ? this.state.errorMessage : DEFAULT_ERROR} | ||
</div> | ||
) | ||
} | ||
|
||
return this.props.children | ||
} | ||
} | ||
|
||
export default ErrorBoundary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters