ssr shared table between pages
Unanswered
Spectacled Caiman posted this in #help-forum
Spectacled CaimanOP
Hi guys, basically I can’t understand the new SSR of nextjs 14.
I need to create a dashboard with 5 pages, each page has a datatable… can the datatable be shared as a unique component? Because it has dynamic pagination, order, filters etc… so how datatable can access the page query on a select sort or page changes?
Thanks 🙏
I need to create a dashboard with 5 pages, each page has a datatable… can the datatable be shared as a unique component? Because it has dynamic pagination, order, filters etc… so how datatable can access the page query on a select sort or page changes?
Thanks 🙏
15 Replies
Plott Hound
Hi. yes React will let you reuse the component in multiple places. you just need to fetch the data you want to display and pass it to your table
Spectacled CaimanOP
Yes but not with ssr nextjs
In this case is client side not ssr
Plott Hound
do you mean the table needs 'use client'
in app dir you want to make a folder in your
in this page file you'll first want to get the data you want to use in the table.
next you'll make a component or just use your table component if its marked as 'use client' already. this file need 'use client' at the top. we are going to add the table component in here.
all thats left is to pass the data we fetched in the server component down to the client component where we will display the table
app folder for the name of the route eg /app/invoices/ then add a page.js or page.tsx into it. in this page file you'll first want to get the data you want to use in the table.
next you'll make a component or just use your table component if its marked as 'use client' already. this file need 'use client' at the top. we are going to add the table component in here.
all thats left is to pass the data we fetched in the server component down to the client component where we will display the table
Spectacled CaimanOP
I mean, if i have 5 pages with tables, of course i cannot create 5 tables… i will create 1 table and include it in 5 pages, right?
Then, on table interaction, it should re-make the api call to backend to fetch data…
But it cannot fetch data again because page is ssr, and table component is csr
Then, on table interaction, it should re-make the api call to backend to fetch data…
But it cannot fetch data again because page is ssr, and table component is csr
Plott Hound
you will only have one table component marked as 'use client' that has the table component in it. and you will import this into all the pages that will display the table. you need multiple pages/routes for web pages but only one component for the table. always think 'DRY' (Dont repeat yourself )
Spectacled CaimanOP
Correct, but how page will reload data when i change pagination on table?
All examples i saw on internet used table inside the page
Plott Hound
ok for that you can
useSearchParams https://nextjs.org/docs/app/api-reference/functions/use-search-paramsbasically the url will be used to instruct what data should be fetched
so when you do something in the table it will update the url param and the fetch in your server component will automatically refetch the data with the desired filters
this is an excellent example to show you the general idea: https://nextjs.org/learn/dashboard-app/adding-search-and-pagination
Spectacled CaimanOP
Ok thanks, do you know maybe some open source admin panel that does it? Maybe using tanstack
Plott Hound
im not sure what you are asking for sorry