Consider this Next.js Error.tsx component used for route errors. What will it display if the error status code is 404?
import { useEffect } from 'react'; import { useRouter } from 'next/router'; export default function Error({ error }: { error: { statusCode: number } }) { const router = useRouter(); useEffect(() => { if (error.statusCode === 404) { console.log('Page not found'); } }, [error.statusCode]); return ( <main role="alert" aria-live="assertive" style={{ padding: '2rem', textAlign: 'center' }}> {error.statusCode === 404 ? ( <h1>Sorry, this page does not exist.</h1> ) : ( <h1>Oops! Something went wrong.</h1> )} </main> ); }
Look at the condition that checks error.statusCode inside the return statement.
The component checks if error.statusCode is 404. If yes, it renders the message "Sorry, this page does not exist." Otherwise, it shows a generic error message.
Review the following snippet from an Error.tsx component. What does the useEffect hook do here?
import { useEffect } from 'react'; export default function Error({ error }: { error: { statusCode: number } }) { useEffect(() => { if (error.statusCode === 404) { console.log('Page not found'); } }, [error.statusCode]); return <div>Error occurred</div>; }
Check what the console.log inside useEffect does and when it runs.
The useEffect runs after the component mounts or when error.statusCode changes. It logs a message only if the status code is 404.
Identify which option contains a syntax error that would prevent this Next.js error component from compiling.
export default function Error({ error }: { error: { statusCode: number } }) { return ( <main> {error.statusCode === 404 ? ( <h1>Page not found</h1> ) : ( <h1>Unexpected error</h1> )} </main> ); }
Look for missing closing tags in JSX syntax.
Using an opening fragment tag <> without a closing </> causes a syntax error in JSX.
Given this Error.tsx component, what will it display when the error status code is 500?
export default function Error({ error }: { error: { statusCode: number } }) { return ( <main role="alert" aria-live="assertive"> {error.statusCode === 404 ? ( <h1>Page not found</h1> ) : error.statusCode === 401 ? ( <h1>Unauthorized access</h1> ) : ( <h1>Unexpected error occurred</h1> )} </main> ); }
Check the conditional rendering for status codes other than 404 and 401.
The component renders "Unexpected error occurred" for any status code not 404 or 401, including 500.
Examine this Error.tsx component. It should show the error status code but instead shows nothing. What is the cause?
export default function Error({ error }: { error: { statusCode: number } }) { return ( <main> <h1>Error code: {error.statusCode}</h1> </main> ); } // Usage example: // <Error error={404} />
Check how the error prop is passed versus how it is used inside the component.
The component expects error to be an object with a statusCode property, but the usage passes a number directly, causing error.statusCode to be undefined.