FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
ViewportErrorBoundary.tsx
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright (c) 2026-present FERS Contributors (see AUTHORS.md).
3
4import React from 'react';
5
6interface ViewportErrorBoundaryProps {
7 children: React.ReactNode;
8 fallback?: React.ReactNode;
9 onError?: (error: Error) => void;
10 resetKey?: string | number;
11}
12
13interface ViewportErrorBoundaryState {
14 hasError: boolean;
15}
16
17export class ViewportErrorBoundary extends React.Component<
18 ViewportErrorBoundaryProps,
19 ViewportErrorBoundaryState
20> {
21 override state: ViewportErrorBoundaryState = {
22 hasError: false,
23 };
24
25 static getDerivedStateFromError(): ViewportErrorBoundaryState {
26 return {
27 hasError: true,
28 };
29 }
30
31 override componentDidCatch(error: Error): void {
32 this.props.onError?.(error);
33 }
34
35 override componentDidUpdate(
36 prevProps: Readonly<ViewportErrorBoundaryProps>
37 ): void {
38 if (this.state.hasError && prevProps.resetKey !== this.props.resetKey) {
39 this.setState({ hasError: false });
40 }
41 }
42
43 override render(): React.ReactNode {
44 if (this.state.hasError) {
45 return this.props.fallback ?? null;
46 }
47
48 return this.props.children;
49 }
50}