Class ErrorBoundary

A React component that acts as an error boundary to catch JavaScript errors anywhere in its child component tree. It displays a fallback UI when an error is caught.

Hierarchy

  • PureComponent<IErrorBoundaryProps, IErrorBoundaryInnerState>
    • ErrorBoundary

Constructors

Properties

context: any

If using the new style context, re-declare this in your class to be the React.ContextType of your static contextType. Should be used with type annotation or static contextType.

static contextType = MyContext
// For TS pre-3.7:
context!: React.ContextType<typeof MyContext>
// For TS 3.7 and above:
declare context: React.ContextType<typeof MyContext>
props: Readonly<IErrorBoundaryProps> & Readonly<{ children?: ReactNode }>
refs: { [key: string]: ReactInstance }
state: Readonly<IErrorBoundaryInnerState>
contextType?: Context<any>

If set, this.context will be set at runtime to the current value of the given Context.

Usage:

type MyContext = number
const Ctx = React.createContext<MyContext>(0)

class Foo extends React.Component {
static contextType = Ctx
context!: React.ContextType<typeof Ctx>
render () {
return <>My context's value: {this.context}</>;
}
}

Methods

  • Handles the error and updates the state.

    Parameters

    • error: Error

      The error that was thrown.

    • info: ErrorInfo

      Additional information about the error.

    Returns void

  • Called immediately after a component is mounted. Setting state here will trigger re-rendering.

    Returns void

  • Called immediately after updating occurs. Not called for the initial render.

    The snapshot is only present if getSnapshotBeforeUpdate is present and returns non-null.

    Parameters

    • prevProps: Readonly<IErrorBoundaryProps>
    • prevState: Readonly<IErrorBoundaryInnerState>
    • Optionalsnapshot: any

    Returns void

  • Called immediately before a component is destroyed. Perform any necessary cleanup in this method, such as cancelled network requests, or cleaning up any DOM elements created in componentDidMount.

    Returns void

  • Parameters

    • Optionalcallback: () => void

    Returns void

  • Runs before React applies the result of render to the document, and returns an object to be given to componentDidUpdate. Useful for saving things such as scroll position before render causes changes to it.

    Note: the presence of getSnapshotBeforeUpdate prevents any of the deprecated lifecycle events from running.

    Parameters

    • prevProps: Readonly<IErrorBoundaryProps>
    • prevState: Readonly<IErrorBoundaryInnerState>

    Returns any

  • Renders the component.

    Returns Element

    The rendered component.

  • Type Parameters

    • K extends "error"

    Parameters

    • state:
          | IErrorBoundaryInnerState
          | (
              prevState: Readonly<IErrorBoundaryInnerState>,
              props: Readonly<IErrorBoundaryProps>,
          ) => IErrorBoundaryInnerState | Pick<IErrorBoundaryInnerState, K>
          | Pick<IErrorBoundaryInnerState, K>
    • Optionalcallback: () => void

    Returns void

  • Called to determine whether the change in props and state should trigger a re-render.

    Component always returns true. PureComponent implements a shallow comparison on props and state and returns true if any props or states have changed.

    If false is returned, Component#render, componentWillUpdate and componentDidUpdate will not be called.

    Parameters

    • nextProps: Readonly<IErrorBoundaryProps>
    • nextState: Readonly<IErrorBoundaryInnerState>
    • nextContext: any

    Returns boolean

  • Updates the state with the error.

    Parameters

    • error: Error

      The error that was thrown.

    Returns { error: Error }

    The updated state.