Files
next.js/errors/class-component-in-server-component.mdx
T
6f4352993d docs: add and unify notes to error pages (#72838)
## Description
Unify the note format according to the [contribution
guide](https://nextjs.org/docs/community/contribution-guide#notes).
In addition, add missing bullet points to useful links.

### Improving Documentation

- [x] Run `pnpm prettier-fix` to fix formatting issues before opening
the PR.
- [x] Read the Docs Contribution Guide to ensure your contribution
follows the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

---------

Co-authored-by: Delba de Oliveira <[email protected]>
2024-11-19 09:44:42 +00:00

58 lines
1.1 KiB
Plaintext

---
title: 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 filename="app/page.js"
export default class Page extends React.Component {
render() {
return <p>Hello world</p>
}
}
```
### After
```jsx filename="app/page.js"
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 filename="app/page.js"
export default class Page extends React.Component {
render() {
return <p>Hello world</p>
}
}
```
### After
```jsx filename="app/page.js"
'use client'
export default class Page extends React.Component {
render() {
return <p>Hello world</p>
}
}
```
## Useful Links
- [Server Components](/docs/app/building-your-application/rendering/server-components)