Is it okay to fetch server side and pass the data to a client component?
Unanswered
Amur catfish posted this in #help-forum
Amur catfishOP
Hi, i have a question.
I was thinking about fetching data (lets say a list of users) in a server component. What happens if I want to edit/add/delete a user?
I come from react where I simply store the data in a state and modifying it would re render the component.
What if I fetch the data server side and pass it to a client component where I actually add it to a state and work from there?
I was thinking about fetching data (lets say a list of users) in a server component. What happens if I want to edit/add/delete a user?
I come from react where I simply store the data in a state and modifying it would re render the component.
What if I fetch the data server side and pass it to a client component where I actually add it to a state and work from there?
13 Replies
@Amur catfish Hi, i have a question.
I was thinking about fetching data (lets say a list of users) in a server component. What happens if I want to edit/add/delete a user?
I come from react where I simply store the data in a state and modifying it would re render the component.
What if I fetch the data server side and pass it to a client component where I actually add it to a state and work from there?
for the crud operation, I don't think you need to store the data in client state?
// todos/page.tsx
export default async function TodosPage() {
const todos = await getTodos()
return <div>...</div>
}
// todos/create/page.tsx
export default async function CreateTodoPage() {
return <form action={createTodo}></form>
}
// todos/[id]/edit/page.tsx
export default async function EditTodPage({params}: {params: {id:string}}) {
const todo = await getTodoById(params.id)
return <div>...</div>
}
// todos/[id]/delete/page.ts
export default async function DeleteTodoPage({params}: {params: {id:string}}) {
return <form action={deleteTodo}></form>
}Amur catfishOP
What if I want to create them in the todos page? Using a modal or something
@Amur catfish What if I want to create them in the todos page? Using a modal or something
create the modal on the create todo page
and use server action
Amur catfishOP
I dont want to travel to the create todo page
I want to stay at the todos page
yea you can
Amur catfishOP
Then? Would the server action perform a "re render"?
why would you need the todos state for creating a todo?
Amur catfishOP
To add the new todo to the list
doesn't need if you use
revalidatePath in the server actionbut yeah, It is totally fine to fetch the data server side and pass it to client