Here is my current attempt on how to properly type a React ErrorBoundary
class component in Typescript:
import React from "react";
import ErrorPage from "./ErrorPage";
import type { Store } from "redux"; // I'M PASSING THE REDUX STORE AS A CUSTOM PROP
interface Props {
store: Store // THIS IS A CUSTOM PROP THAT I'M PASSING
}
interface State { // IS THIS THE CORRECT TYPE FOR THE state ?
hasError: boolean
}
class ErrorBoundary extends React.Component<Props,State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error): State {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
componentDidCatch(error, errorInfo: React.ErrorInfo): void {
// You can also log the error to an error reporting service
// logErrorToMyService(error, errorInfo);
console.log("error: " + error);
console.log("errorInfo: " + JSON.stringify(errorInfo));
console.log("componentStack: " + errorInfo.componentStack);
}
render(): React.ReactNode {
if (this.state.hasError) {
// You can render any custom fallback UI
return(
<ErrorPage
message={"Something went wrong..."}
/>
);
}
return this.props.children;
}
}
export default ErrorBoundary;
This question is about the types for this ErrorBoundary
class component. I'm breaking it into parts to make it easier.
PART A: Types for props and state
Is what I'm doing right?
interface Props {
store: Store // THIS IS A CUSTOM PROP THAT I'M PASSING
}
interface State { // IS THIS THE CORRECT TYPE FOR THE state ?
hasError: boolean
}
class ErrorBoundary extends React.Component<Props,State> {}
PART B: getDerivedStateFromError(error)
What type should I choose for the error
parameter? The return type should be the State
type, right?
static getDerivedStateFromError(error): State {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
PART C: componentDidCatch(error, errorInfo: React.React.ErrorInfo)
What type should I choose for the error
parameter? For the errorInfo
, React does have a ErrorInfo
type that seems to be correct. Is it? The return type should be void
, correct?
componentDidCatch(error, errorInfo: React.ErrorInfo): void {
console.log("error: " + error);
console.log("errorInfo: " + JSON.stringify(errorInfo));
console.log("componentStack: " + errorInfo.componentStack);
}
PART D: the render() method
Should the return type be ReactNode
?
render(): React.ReactNode {
if (this.state.hasError) {
// You can render any custom fallback UI
return(
<ErrorPage
message={"Something went wrong..."}
/>
);
}
return this.props.children;
}
import type
syntax was release in a more recent version of Typescript, not sure which one, but it won't work in some older versions. – StilbiteErrorBoundary
. But I guess I can access the store withuseStore()
fromreact-redux
since theErrorBoundary
will be inside the<Provider store={store}>
. Thanks. EDIT: actually I can't useuseStore
inside a class component. Just found that out. Got that error message: React Hook "useStore" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function. – Stilbite