Skip to content

fix(react): Make fallback render types more accurate #7198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions packages/react/src/errorboundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const UNKNOWN_COMPONENT = 'unknown';

export type FallbackRender = (errorData: {
error: Error;
componentStack: string | null;
eventId: string | null;
componentStack: string;
eventId: string;
resetError(): void;
}) => React.ReactElement;

Expand Down Expand Up @@ -48,11 +48,17 @@ export type ErrorBoundaryProps = {
beforeCapture?(scope: Scope, error: Error | null, componentStack: string | null): void;
};

type ErrorBoundaryState = {
componentStack: React.ErrorInfo['componentStack'] | null;
error: Error | null;
eventId: string | null;
};
type ErrorBoundaryState =
| {
componentStack: null;
error: null;
eventId: null;
}
| {
componentStack: React.ErrorInfo['componentStack'];
error: Error;
eventId: string;
};

const INITIAL_STATE = {
componentStack: null,
Expand Down Expand Up @@ -133,12 +139,17 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta

public render(): React.ReactNode {
const { fallback, children } = this.props;
const { error, componentStack, eventId } = this.state;
const state = this.state;

if (error) {
if (state.error) {
let element: React.ReactElement | undefined = undefined;
if (typeof fallback === 'function') {
element = fallback({ error, componentStack, resetError: this.resetErrorBoundary, eventId });
element = fallback({
error: state.error,
componentStack: state.componentStack,
resetError: this.resetErrorBoundary,
eventId: state.eventId,
});
} else {
element = fallback;
}
Expand Down