Next.js Discord

Discord Forum

Good way to coordinates server and client

Answered
Eurasian Blackbird posted this in #help-forum
Open in Discord
Avatar
Eurasian BlackbirdOP
Hello, thanks for everyone who will try to help me !
So my question is the following : how to update the UI based on the user input.
Let's say I have a list of product that I want to be fetched (using an orm) on the server. I then want it to be displayed. For now I can stay with basic server component.
export default async function page() {
  const products = await db.products.findMany();
  return <ul>
              {products.map(product => {
                  return <li key={product.id}>{product.name}</li>
          </ul>
}

But now, i want the user to be able to filter that list (& to sort it). So I need to update my products variable according to the user interactivity. I'm not sure how to do this.
It now looks like that but I don't know how to complete it
export default async function page() {
  const products = await db.products.findMany();
  return <ul>
              <SomeFilteRingClientComponent />
              {products.map(product => {
                  return <li key={product.id}>{product.name}</li>
          </ul>
}
Answered by not-milo.tsx
Well, you have to use client components if you want to implement this kind of functionality...

You can fetch your products on the server and pass it to a client component that uses search parameters or a simple React state to filter them out based on user input.

To know more about the useSearchParams hook read the docs here: https://nextjs.org/docs/app/api-reference/functions/use-search-params
View full answer

5 Replies

Avatar
Eurasian BlackbirdOP
I would like to avoid client side filtering & route handlers if possible
Avatar
Well, you have to use client components if you want to implement this kind of functionality...

You can fetch your products on the server and pass it to a client component that uses search parameters or a simple React state to filter them out based on user input.

To know more about the useSearchParams hook read the docs here: https://nextjs.org/docs/app/api-reference/functions/use-search-params
Answer
Avatar
@Eurasian Blackbird Thank you !
Avatar
No problem ✌🏻