Unverified Commit 9a5c8ceb authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Improve errors caused by Class Component rendered in a server component. (#44726)

Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
Co-authored-by: 's avatarSteven <steven@ceriously.com>
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 03e5f5e1
# React Class component rendered in a Server Component
#### Why This Error Occurred
You are rendering a React Class Component in a Server Component, `React.Component` and `React.PureComponent` only works in Client Components.
#### Possible Ways to Fix It
Use a Function Component.
##### Before
```jsx
export default class Page extends React.Component {
render() {
return <p>Hello world</p>
}
}
```
##### After
```jsx
export default function Page() {
return <p>Hello world</p>
}
```
Mark the component rendering the React Class Component as a Client Component by adding `'use client'` at the top of the file.
##### Before
```jsx
export default class Page extends React.Component {
render() {
return <p>Hello world</p>
}
}
```
##### After
```jsx
'use client'
export default class Page extends React.Component {
render() {
return <p>Hello world</p>
}
}
```
### Useful Links
[Server and Client Components](https://beta.nextjs.org/docs/rendering/server-and-client-components)
...@@ -781,6 +781,10 @@ ...@@ -781,6 +781,10 @@
{ {
"title": "react-client-hook-in-server-component", "title": "react-client-hook-in-server-component",
"path": "/errors/react-client-hook-in-server-component.md" "path": "/errors/react-client-hook-in-server-component.md"
},
{
"title": "class-component-in-server-component",
"path": "/errors/class-component-in-server-component.md"
} }
] ]
} }
......
...@@ -21,6 +21,20 @@ function setMessage(error: Error, message: string): void { ...@@ -21,6 +21,20 @@ function setMessage(error: Error, message: string): void {
} }
export function formatServerError(error: Error): void { export function formatServerError(error: Error): void {
if (
error.message.includes(
'Class extends value undefined is not a constructor or null'
)
) {
setMessage(
error,
`${error.message}
This might be caused by a React Class component being rendered in a server component, React Class components only works in Client Components. Read more: https://nextjs.org/docs/messages/class-component-in-server-component`
)
return
}
if (error.message.includes('createContext is not a function')) { if (error.message.includes('createContext is not a function')) {
setMessage( setMessage(
error, error,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment