Why does the error.js page need to be a client component?
Answered
Giant panda posted this in #help-forum
Giant pandaOP
I could not find anything about this in the docs or online. I am using the app router and have a page.js server component that throws an error. The corresponding error.js in the same folder should be able to catch the error on the server-side and render a different component instead, but when I try defining the error page without
Does anyone know why it needs to be a client component? Is it because error boundaries only work in client components?
'use client', I get an error that it must be a client component.Does anyone know why it needs to be a client component? Is it because error boundaries only work in client components?
Answered by joulev
Error boundaries are class components (this is a React limitation), and class components are not supported in server components
3 Replies
@Giant panda I could not find anything about this in the docs or online. I am using the app router and have a page.js server component that throws an error. The corresponding error.js in the same folder should be able to catch the error on the server-side and render a different component instead, but when I try defining the error page without `'use client'`, I get an error that it must be a client component.
Does anyone know *why* it needs to be a client component? Is it because error boundaries only work in client components?
Error boundaries are class components (this is a React limitation), and class components are not supported in server components
Answer
Giant pandaOP
Thanks for the quick answer! That makes sense. Are there any plans to lift this limitation in the future? It’s not a big deal, but would be nice to be able to catch errors Server-side as well
I don’t know. Nextjs themselves can’t do it, it’s React’s job to implement and support functional component error boundaries. To my knowledge, only class components have something like [
componentDidCatch](https://react.dev/reference/react/Component#componentdidcatch) which is required for error boundaries.