UNPKG

@suspensive/react

Version:

Suspensive interfaces for react

80 lines (79 loc) 4.5 kB
import { PropsWithoutChildren } from "./utility-types/PropsWithoutChildren.cjs"; import { ConstructorType } from "./utility-types/ConstructorType.cjs"; import * as _$react_jsx_runtime0 from "react/jsx-runtime"; import { ComponentProps, ComponentType, ErrorInfo, ForwardRefExoticComponent, FunctionComponent, PropsWithChildren, ReactNode } from "react"; //#region src/ErrorBoundary.d.ts interface ErrorBoundaryHandle { /** * when you want to reset caught error, you can use this reset */ reset: () => void; } interface ErrorBoundaryFallbackProps<TError extends Error = Error> extends ErrorBoundaryHandle { /** * when ErrorBoundary catch error, you can use this error */ error: TError; } type ErrorTypeGuard<TError extends Error> = (error: Error) => error is TError; type ErrorValidator = (error: Error) => boolean; type ErrorMatcher = boolean | ConstructorType<Error> | ErrorTypeGuard<Error> | ErrorValidator; type InferErrorByErrorMatcher<TErrorMatcher extends ErrorMatcher> = TErrorMatcher extends ConstructorType<infer TErrorOfConstructorType extends Error> ? TErrorOfConstructorType : TErrorMatcher extends ErrorTypeGuard<infer TErrorOfTypeGuard extends Error> ? TErrorOfTypeGuard : Error; type ShouldCatch = ErrorMatcher | [ErrorMatcher, ...ErrorMatcher[]]; /** * Main type inference from shouldCatch */ type InferError<TShouldCatch extends ShouldCatch> = TShouldCatch extends readonly ErrorMatcher[] ? InferErrorByErrorMatcher<TShouldCatch[number]> extends never ? Error : InferErrorByErrorMatcher<TShouldCatch[number]> : TShouldCatch extends ErrorMatcher ? InferErrorByErrorMatcher<TShouldCatch> : Error; type ErrorBoundaryProps<TShouldCatch extends ShouldCatch = true> = PropsWithChildren<{ /** * an array of elements for the ErrorBoundary to check each render. If any of those elements change between renders, then the ErrorBoundary will reset the state which will re-render the children */ resetKeys?: unknown[]; /** * when ErrorBoundary is reset by resetKeys or fallback's props.reset, onReset will be triggered */ onReset?: () => void; /** * when ErrorBoundary catch error, onError will be triggered */ onError?: (error: InferError<TShouldCatch>, info: ErrorInfo) => void; /** * when ErrorBoundary catch error, fallback will be render instead of children */ fallback: ReactNode | FunctionComponent<ErrorBoundaryFallbackProps<InferError<TShouldCatch>>>; /** * determines whether the ErrorBoundary should catch errors based on conditions * @default true */ shouldCatch?: TShouldCatch; }>; /** * This component provides a simple and reusable wrapper that you can use to wrap around your components. Any rendering errors in your components hierarchy can then be gracefully handled. * @see {@link https://suspensive.org/docs/react/ErrorBoundary Suspensive Docs} */ declare const ErrorBoundary: (<TShouldCatch extends ShouldCatch>(props: ErrorBoundaryProps<TShouldCatch> & React.RefAttributes<ErrorBoundaryHandle>) => ReturnType<ForwardRefExoticComponent<ErrorBoundaryProps<TShouldCatch>>>) & { displayName: string; with: <TProps extends ComponentProps<ComponentType> = Record<string, never>, TShouldCatch extends ShouldCatch = ShouldCatch>(errorBoundaryProps: PropsWithoutChildren<ErrorBoundaryProps<TShouldCatch>>, Component: ComponentType<TProps>) => ((props: TProps) => _$react_jsx_runtime0.JSX.Element) & { displayName: string; }; Consumer: ({ children }: { children: (errorBoundary: ReturnType<typeof useErrorBoundary>) => ReactNode; }) => _$react_jsx_runtime0.JSX.Element; }; /** * This hook provides a simple and reusable wrapper that you can use to wrap around your components. Any rendering errors in your components hierarchy can then be gracefully handled. * @see {@link https://suspensive.org/docs/react/ErrorBoundary#useerrorboundary Suspensive Docs} */ declare const useErrorBoundary: <TError extends Error = Error>() => { setError: (error: TError) => void; }; /** * This hook allows you to access the reset method and error objects without prop drilling. * @see {@link https://suspensive.org/docs/react/ErrorBoundary#useerrorboundaryfallbackprops Suspensive Docs} */ declare const useErrorBoundaryFallbackProps: <TError extends Error = Error>() => ErrorBoundaryFallbackProps<TError>; //#endregion export { ErrorBoundary, ErrorBoundaryFallbackProps, ErrorBoundaryProps, useErrorBoundary, useErrorBoundaryFallbackProps }; //# sourceMappingURL=ErrorBoundary.d.cts.map