Next.js Discord

Discord Forum

How to make server request that depends on client side data?

Unanswered
Blanc de Hotot posted this in #help-forum
Open in Discord
Blanc de HototOP
Hi there, I'm building a calendar-type app and then entire layout depends on client side state, specifically the current selected month.

I'm confused on how to trigger a server side request that depends on that.

const [currentMonth, setCurrentMonth] = React.useState(new Date()) 
const { data } = await getEntriesByMonth(currentMonth)

I'll also be updating currentMonth through the app when a user navigates.

If there's any alternatives on how to handle this, would be appreciated!

12 Replies

The best thing is to use next server functionality how you supposed to.

So use them as follows:
Server Component = Fetch Data (and more..)
Server Actions = Mutate Data (and more..)
Client Component = Interactivity

If you have a calendar month, fetch this month clientside and pass the data to your other components. Within these other components, you can add interactivity like drag and drop and combine the onDrop for example with a server actions (mutate data & revalidate path/tags). After you done that, your server directly returns the new state (month)-state and show it on your page.

You combined now Server Components, to fetch your month, client components to add interactivity to it and server mutations to save data and get the new page through revalidate path/tags. @Blanc de Hotot
Blanc de HototOP
@B33fb0n3 A little lost, could you share an example of how you would structure this?
Are you saying I don't pass currentMonth at page level as that would be a server component, and I just get all the entries?
oh I'm supposed to -> getEntriesByMonth(new Date())

and then mutate that data with a server action, which I can pass through client side state?

will i not be defining the action within the Server component anyways?
When I would be you, I would create a dynamic page for the months. Inside it, I would fetch the month from the dynamic fragment. This fetched month contains appointments and other data and you can pass these appointsments for example to your client components, to create interactivity. For example: clicks on it, selectables, ...
If you want to switch to the next month, just redirect/push the user to the next month. The dynamic server page will be triggerted (fetching the month) and show the results (from the next month) in your calendar
You can do exactly the same with days, weeks, month, years. Keep in mind to now overfetch (fetching all appointments for one year might not be the best idea)
Blanc de HototOP
still lost sorry, I guess I just need to store state somewhere else, like the URL?

managed to build the app all client side very easily and thinking i just stick w thay
@Blanc de Hotot still lost sorry, I guess I just need to store state somewhere else, like the URL? managed to build the app all client side very easily and thinking i just stick w thay
To build a App just clientside is easy… but it’s also like react.. and react ≠ NextJs. So you might want to also use the NextJs features. Next is build for that
Blanc de HototOP
@Ray Would you have an example of using SWR with Server Action within client component?
Blanc de HototOP
Alright things look good. Everything works but I’m realizing it might be better to make a request per calendar day, so you only end up revalidating that that when it updates rather than everything