Next.js Discord

Discord Forum

How To Render A Component After Server Component Loaded ?

Unanswered
LaPerm posted this in #help-forum
Open in Discord
LaPermOP
so i have a component in my navbar (server component and in layout) and i want to change it that there is a placeholder for it and remove it from taskbar
then render it from a client component
how can i do this ?

12 Replies

are you talking about a skeleton element that you're using as a placeholder while you wait for data to load?
@not-milo.tsx are you talking about a skeleton element that you're using as a placeholder while you wait for data to load?
LaPermOP
not exactly
i wan't a div with some id and i want to append a component to it in my client component
You can render components conditionally based on some context state. From your client component you can update the context state and then use it in the navbar to render another component inside it if this is what you're looking for
It's hard to say given the somewhat generic request. It would help if you shard a screenshot of the app or it's folder structure with annotations
LaPermOP
in that case i have to make my navbar a client component and i don't want to convert it
i'm looking for something like render(document.getElementById("id") , <Component />)
You don't have to make it a client component. Just add a client component inside it that doesn't render anything until the context state changes to whatever value you set
@not-milo.tsx You don't have to make it a client component. Just add a client component inside it that doesn't render anything until the context state changes to whatever value you set
LaPermOP
yea but it's in my layout and where i need to use it is in my page.js and inside <Main /> component
so in order to make it work i have to make layout client component so i can use context
No, you're misunderstanding how client and server components work...
You can have a server component nested inside a client context and still keep it as an rsc. Read this section from the documentation: https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#supported-pattern-passing-server-components-to-client-components-as-props
No, if you place the 'use client' directive in a file you can't import server components in it anymore. You can only pass server components to client components as props.
This is allowed tho:

import { ServerComponentA, ServerComponentB } from './components/server'
import { ClientComponent } from './components/client'

export default async function Page() {
  return (
    <ServerComponentA>
      <ClientComponent>
        <ServerComponentB />
      </ClientComponent>
    </ServerComponentA>
  )
}
LaPermOP
thanks alot