Listing 13-9: Specifying a fallback component with react-error-boundary
import ErrorBoundary from 'react-error-boundary';
function ErrorFallback({error}) {
return (
<div role="alert">
<p>Something went wrong:</p>
<SyntaxHighlighter language="javascript" style={github}>{error.message}</SyntaxHighlighter>
</div>
)
}
function BadComponentContainer(){
return (
<ErrorBoundary
FallbackComponent={ErrorFallback}>
<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.