Search code examples
javascriptreactjstypescriptnext.jsserver-side-rendering

How can i use server component in client component?


i wanna to use server component in client component. when i use use client in parent component all child component convert to client component i wanna render server component in parent client component its possible or not.

i wanna render some components in server but when use server components in client component all server components render on the client side. But i need to render from the server side :?

Has anyone ever tried something like this? Or did he implement something like this?


Solution

  • You can't import server Components within Client Components - quoting to Next.js docs

    The following pattern is not supported. You cannot import a Server Component into a Client Component

    However, you can accept Server Components into Child Components via the children prop. Example from the docs

    'use client'
     
    import { useState } from 'react'
     
    export default function ClientComponent({
      children,
    }: {
      children: React.ReactNode
    }) {
      const [count, setCount] = useState(0)
     
      return (
        <>
          <button onClick={() => setCount(count + 1)}>{count}</button>
          {children}
        </>
      )
    }
    

    The components will render independently.