rsnext/errors/class-component-in-server-component.md
Hannes Bornö 9a5c8cebf0
Improve errors caused by Class Component rendered in a server component. (#44726)
Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
Co-authored-by: Steven <steven@ceriously.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-01-10 14:47:20 +01:00

1,012 B

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
export default class Page extends React.Component {
  render() {
    return <p>Hello world</p>
  }
}
After
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
export default class Page extends React.Component {
  render() {
    return <p>Hello world</p>
  }
}
After
'use client'
export default class Page extends React.Component {
  render() {
    return <p>Hello world</p>
  }
}

Server and Client Components