react-error-boundary icon indicating copy to clipboard operation
react-error-boundary copied to clipboard

type ErrorBoundaryPropsWithFallback allows fallback to be null

Open adam-zhu opened this issue 3 years ago • 4 comments

Possible I'm missing something here, but it looks like this type isn't correct.

  • react-error-boundary version: 4.0.11
  • node version: 16.19.0
  • npm version: 9.6.7

What you did: Invoked <ErrorBoundary fallback={null} />. Looking at the type, null is allowed. That implies that passing null would just render null as the fallback.

What happened: Didn't catch render errors in child components.

Reproduction repository: github.com/bvaughn/react-error-boundary

Problem description:

export type ErrorBoundaryPropsWithFallback = ErrorBoundarySharedProps & {
    fallback: ReactElement<unknown, string | FunctionComponent | typeof Component> | null;
    FallbackComponent?: never;
    fallbackRender?: never;
};

Type allows fallback to be null when it apparently shouldn't.

Suggested solution: Remove null from the type.

adam-zhu avatar Sep 20 '23 17:09 adam-zhu

If you want to render null with the way the library currently works, I think you have to pass a component that renders null (e.g. () => null)

Presently null kind of acts like a passthrough (so you could use the boundary for e.g. logging only, using onError)

bvaughn avatar Sep 20 '23 17:09 bvaughn

For me, passing null doesn't function as a passthrough because any caught errors are bubbled up and break the app.

Looking at the code, this is expected: https://github.com/bvaughn/react-error-boundary/blob/master/src/ErrorBoundary.ts#L87

    if (didCatch) {
      const props: FallbackProps = {
        error,
        resetErrorBoundary: this.resetErrorBoundary,
      };

      if (isValidElement(fallback)) {
        childToRender = fallback;
      } else if (typeof fallbackRender === "function") {
        childToRender = fallbackRender(props);
      } else if (FallbackComponent) {
        childToRender = createElement(FallbackComponent, props);
      } else {
        if (isDevelopment) {
          console.error(
            "react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop"
          );
        }

        throw error;
      }
    }

adam-zhu avatar Sep 20 '23 17:09 adam-zhu

Ah, yeah that makes sense I guess I was confusing React's undefined fallback prop.

I don't feel strongly about this (and probably also won't work on it)

bvaughn avatar Sep 20 '23 17:09 bvaughn

4.0.12 will render null if you specify fallback={null}

bvaughn avatar Dec 15 '23 14:12 bvaughn