Complete the code to import the React hook needed for error boundaries.
import React, { [1] } from 'react';
The useErrorHandler hook from Next.js is used to handle errors in client-side error boundaries.
Complete the code to catch errors in a client component using the error boundary hook.
const errorHandler = [1]();The useErrorHandler hook returns a function to catch and handle errors in client components.
Fix the error in the code to properly throw an error inside a client component.
if (hasError) { [1](new Error('Something went wrong')); }
Inside a client component using error boundaries, you call the errorHandler function with the error to trigger the boundary.
Fill both blanks to create a client component that uses error boundaries correctly.
"use client"; import { [1] } from 'react-error-boundary'; export default function ErrorBoundaryWrapper({ children }) { return <[2] fallback={<p>Oops! An error occurred.</p>}>{children}</[2]>; }
The ErrorBoundary component from react-error-boundary wraps children and shows a fallback UI on error.
Fill all three blanks to handle an error and reset the error boundary on button click.
"use client"; import { useErrorHandler, ErrorBoundary } from 'react-error-boundary'; import { useState } from 'react'; function BuggyComponent() { const handleError = [1](); const [hasError, setHasError] = useState(false); if (hasError) { [2](new Error('Bug detected')); } return <button onClick={() => setHasError(true)}>Cause Error</button>; } export default function App() { return ( <ErrorBoundary fallbackRender={({ resetErrorBoundary }) => ( <div> <p>There was an error.</p> <button onClick=[3]>Try again</button> </div> )}> <BuggyComponent /> </ErrorBoundary> ); }
Use useErrorHandler to get the error handler function, call it as handleError, and reset the boundary with a callback () => resetErrorBoundary().