type ErrorBoundaryPropsWithFallback allows fallback to be null
Possible I'm missing something here, but it looks like this type isn't correct.
react-error-boundaryversion: 4.0.11nodeversion: 16.19.0npmversion: 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.
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)
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;
}
}
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)
4.0.12 will render null if you specify fallback={null}