If I use server components am I not bound to end up with many files?
Answered
Russian Blue posted this in #help-forum
Russian BlueOP
If I understand correctly one of the features of Server components are meant to be used for is fetching data. Now this is great and all, but if I need to create an individual file for each component I want to have interactivity & for the server component its self am I not going to end up with many many files?
Won't this make my stack messy and possibly harder to understand and work on if I have so many files? Is there a different approach to this or is this the expected solution nextjs developers should be taking?
Won't this make my stack messy and possibly harder to understand and work on if I have so many files? Is there a different approach to this or is this the expected solution nextjs developers should be taking?
Answered by joulev
you can export more than one component in a single file. so i just put every client component for a feature in one single file instead of one component one file
7 Replies
@Russian Blue If I understand correctly one of the features of Server components are meant to be used for is fetching data. Now this is great and all, but if I need to create an individual file for each component I want to have interactivity & for the server component its self am I not going to end up with many many files?
Won't this make my stack messy and possibly harder to understand and work on if I have so many files? Is there a different approach to this or is this the expected solution nextjs developers should be taking?
you can export more than one component in a single file. so i just put every client component for a feature in one single file instead of one component one file
Answer
@joulev you can export more than one component in a single file. so i just put every client component for a feature in one single file instead of one component one file
Russian BlueOP
This is true - Do you think it's a common approach to use server components because I feel that client components for everything are still mostly the norm.
@Russian Blue This is true - Do you think it's a common approach to use server components because I feel that client components for everything are still mostly the norm.
server components and client components do very different things. you use both at the same time and which one to use depends on the occasion
@joulev server components and client components do very different things. you use both at the same time and which one to use depends on the occasion
Russian BlueOP
Yes, I agree. But don't most apps people develop completely ignore server components and use purely client components even for fetching data.
@Russian Blue Yes, I agree. But don't most apps people develop completely ignore server components and use purely client components even for fetching data.
server component is not a feature you can use many of the times. only in nextjs and few other frameworks do you even have the option. so it's just another resource that you can make use of when you use nextjs. you can use server components in one part and client components with data fetching in another part in the same app just fine, it still depends a lot on the occasion itself. of course going all in for one of the two all of the time is not fine.
@joulev server component is not a feature you can use many of the times. only in nextjs and few other frameworks do you even have the option. so it's just another resource that you can make use of when you use nextjs. you can use server components in one part _and_ client components with data fetching in another part in the same app just fine, it still depends a lot on the occasion itself. of course going all in for one of the two all of the time is not fine.
Russian BlueOP
Would you say something like this is an acceptable usage of a server component?
import { api } from "@/trpc/server";
import Form from "./form";
export default async function PortfolioBuilder() {
const data = await api.portfolio.get();
return <Form data={data} />;
}