Help with Server Components
Answered
aychar posted this in #help-forum
aycharOP
So I have a client component that has a useState hook, and inside of that component there are server components (
Relevant code:
<BeatmapCard />
) that are rendered by mapping through an array. Whenever the page first loads, it works fine, and I can tell the server components are working because I see the console.log in the terminal. However, once I type in the <SearchBar />
, which changes the state of searchTerm
, the page crashes and I get an error that await can't be used in a client component, which is coming from the BeatmapCard
server component. I have a feeling this is just a logical error, but it would be nice to know what I'm doing wrong here? Relevant code:
"use client";
import React, { useState } from "react";
import BoxContainer from "./BoxContainer";
import SearchBar from "./SearchBar";
import BeatmapCard from "./BeatmapCard";
import BeatmapListContainer from "./BeatmapListContainer";
export default function BeatmapSearchBox() {
const [searchTerm, setSearchTerm] = useState("");
const handleSearchChange = (newSearchTerm) => {
setSearchTerm(newSearchTerm);
};
const ids = [123, 124, 125];
return (
<div>
<BoxContainer>
<div>
<SearchBar onSearchChange={handleSearchChange} />
<p>Search Term: {searchTerm}</p>
<BeatmapListContainer>
{ids.map((beatmap) => {
return <BeatmapCard key={beatmap} id={beatmap} />;
})}
</BeatmapListContainer>
</div>
</BoxContainer>
</div>
);
}
Answered by aychar
okay i figured it out, i was right about it trying to re-render the server component as a client component, so i just wrapped the component export with memo() and it works now
12 Replies
I'm not sure if you can use server component inside a client component.
You can use both components simultaneously in a page, but can't import & use server component in a client component.
aycharOP
https://arc.net/l/quote/jgxktuvh I'm doing this, the beatmaplistcontanier is a client component and im passing the server component as a prop, which as far as i understand from these docs is supported? i feel like the issue could be that once the page re-renders from the state changing, it's trying to re-render the server component as a client component, so is what im doing still probably not the way to go?
ok well hmm the parent component in that example is a server component but it's a client component on mine...
Server components are not supposed to be imported & used as child of client component.
it can be other way tho.
Hope it helps.
aycharOP
okay i figured it out, i was right about it trying to re-render the server component as a client component, so i just wrapped the component export with memo() and it works now
Answer
whatever works for you ðŸ‘
You can mark your own message as solution ig