Download next.js/errors/class-component-in-server-component.mdx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/next.js/errors/class-component-in-server-component.mdx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/next.js/errors/class-component-in-server-component.mdx
-
curl -L -o class-component-in-server-component.mdx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/next.js/errors/class-component-in-server-component.mdx
1.08 kB
| --- | |
| 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/getting-started/server-and-client-components) | |