Listing 13-3: Exporting with an ErrorBoundary
Oops! There's been an error.
import {Component} from 'react';
class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
return { hasError: true };
}
render() {
if (this.state.hasError) {
return <h1>Oops! There's been an error.</h1>;
}
return this.props.children;
}
}
export default ErrorBoundary;
import ErrorBoundary from './ErrorBoundary';
function BadComponentContainer(){
return (
<ErrorBoundary>
<BadComponent />
</ErrorBoundary>
)
}
function BadComponent(){
return (
{oops:"this is not good"}
);
}
export default BadComponentContainer;
Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.
ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.