UNPKG

@liveblocks/react-ui

Version:

A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.

46 lines (43 loc) 1.28 kB
import { jsx } from 'react/jsx-runtime'; import { createContext, Component, useContext } from 'react'; const ErrorBoundaryContext = createContext(null); class ErrorBoundary extends Component { constructor(props) { super(props); this.state = { error: null }; } static getDerivedStateFromError(error) { return { error }; } componentDidCatch(error, errorInfo) { console.error(error, errorInfo); } reset() { this.setState({ error: null }); } render() { if (this.state.error === null) return this.props.children; const error = this.state.error; const reset = this.reset.bind(this); const fallback = this.props.fallback; const Fallback = typeof fallback === "function" ? fallback : () => fallback ?? null; return /* @__PURE__ */ jsx(ErrorBoundaryContext.Provider, { value: { error, reset }, children: /* @__PURE__ */ jsx(Fallback, { error: this.state.error }) }); } } function useErrorBoundary() { const context = useContext(ErrorBoundaryContext); if (context === null) { throw new Error( "useErrorBoundary must be used within an ErrorBoundary component" ); } return context; } export { ErrorBoundary, useErrorBoundary }; //# sourceMappingURL=ErrorBoundary.js.map