Listing 13-8: Providing a reset link in the Error Boundary
import ErrorBoundary from './ErrorBoundary';
import {useState} from 'react';
function SometimesBad(){
const [message,setMessage] = useState();
const handleClick = () => {
const randomNumber = Math.floor(Math.random() * 2);
if (randomNumber === 1){
setMessage({error:"there has been an error"});
} else {
setMessage("great");
}
}
return (
<div>
<button onClick={handleClick}>Mystery Button</button>
{message}
</div>
);
}
export default SometimesBad;
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.