Here is my current attempt on how to properly type a React ErrorBoundary class component in Typescript:
import React from "react";
import ErrorPage from "./ErrorPage";
import type { Store } from "redux";   // I'M PASSING THE REDUX STORE AS A CUSTOM PROP
interface Props {
  store: Store         // THIS IS A CUSTOM PROP THAT I'M PASSING
}
interface State {      // IS THIS THE CORRECT TYPE FOR THE state ?
  hasError: boolean
}
class ErrorBoundary extends React.Component<Props,State> {
  
  constructor(props: Props) {
    super(props);
    this.state = { hasError: false };
  }
  static getDerivedStateFromError(error): State {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
  }
  componentDidCatch(error, errorInfo: React.ErrorInfo): void {
    // You can also log the error to an error reporting service
    // logErrorToMyService(error, errorInfo);
    console.log("error: " + error);
    console.log("errorInfo: " + JSON.stringify(errorInfo));
    console.log("componentStack: " + errorInfo.componentStack);
  }
  render(): React.ReactNode {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return(
        <ErrorPage
          message={"Something went wrong..."}
        />
      );
    }
    return this.props.children;
  }
}
export default ErrorBoundary;
This question is about the types for this ErrorBoundary class component. I'm breaking it into parts to make it easier.
PART A: Types for props and state
Is what I'm doing right?
interface Props {
  store: Store         // THIS IS A CUSTOM PROP THAT I'M PASSING
}
interface State {      // IS THIS THE CORRECT TYPE FOR THE state ?
  hasError: boolean
}
class ErrorBoundary extends React.Component<Props,State> {}
PART B: getDerivedStateFromError(error)
What type should I choose for the error parameter? The return type should be the State type, right?
static getDerivedStateFromError(error): State {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
  }
PART C: componentDidCatch(error, errorInfo: React.React.ErrorInfo)
What type should I choose for the error parameter? For the errorInfo, React does have a ErrorInfo type that seems to be correct. Is it? The return type should be void, correct?
componentDidCatch(error, errorInfo: React.ErrorInfo): void {
    console.log("error: " + error);
    console.log("errorInfo: " + JSON.stringify(errorInfo));
    console.log("componentStack: " + errorInfo.componentStack);
}
PART D: the render() method
Should the return type be ReactNode ?
render(): React.ReactNode {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return(
        <ErrorPage
          message={"Something went wrong..."}
        />
      );
    }
    return this.props.children;
  }
                You can use the following code as an Error Boundary:
import React, { Component, ErrorInfo, ReactNode } from "react";
interface Props {
  children: ReactNode;
}
interface State {
  hasError: boolean;
}
class ErrorBoundary extends Component<Props, State> {
  public state: State = {
    hasError: false
  };
  public static getDerivedStateFromError(_: Error): State {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
  }
  public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error("Uncaught error:", error, errorInfo);
  }
  public render() {
    if (this.state.hasError) {
      return <h1>Sorry.. there was an error</h1>;
    }
    return this.props.children;
  }
}
export default ErrorBoundary;
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With