Next.js Discord

Discord Forum

Server component in Client Component -How to make server update the states of client?

Answered
Southern Martin posted this in #help-forum
Open in Discord
Southern MartinOP
How to make server update the states of client if the server component renders as a child of client component?

22 Replies

@Southern Martin How to make server update the states of client if the server component renders as a child of client component?
you can't just update specific data. The server have it's states and the client have it's states. Some are shared most are not. You can only "update" it while requesting the page again. That can be done with a normal network request or methods like revalidatePath/revalidateTag

If you want to change your data inside a client component, make sure you using a clientside fetching library like SWR or React Query
@B33fb0n3 you can't just update specific data. The server have it's states and the client have it's states. Some are shared most are not. You can only "update" it while requesting the page again. That can be done with a normal network request or methods like revalidatePath/revalidateTag If you want to change your data inside a client component, make sure you using a clientside fetching library like SWR or React Query
Southern MartinOP
I have a ShareModal client which has a ShareButton and RemoveButton server comps. The ShareModal has a list as a state and that list needs to update, when the button server comps make changes to DB via form server actions. I have revalidate path but that reqires a page reload for the list to update, so I am stuck.
Southern MartinOP
The ShareModal component is hidden on the page by default, unless u click on the share icon so I am guessing the ShareModal does not get revaldiated cuz it is hidden by default
Southern MartinOP
it comes from another client component (call it CardShare) that has a state isOpen to decide when to close or open the ShareModal
Yes and the data inside the modal. Where does that come from? Also from the CardShare component?
@B33fb0n3 Yes and the data inside the modal. Where does that come from? Also from the CardShare component?
Southern MartinOP
data inside the modal (the actual list) is gotten inside the ShareModal
@Southern Martin data inside the modal (the actual list) is gotten inside the ShareModal
Let me guess: using a useEffect and maybe a server action or api endpoint?
Southern MartinOP
I am using this:
I just call this function in ShareModal
to get the initial list of shared users
Hmm yea, how do you call the function?
Southern MartinOP
when isOpen or listId changes meaning when the modal gets first opended, this function gets called
Yes, thanks for clarifying that you call the function inside a useEffect. And you seeing common issues with it right now (that’s why this thread exists).

As said inside my first message (see attached) you should use a clientside fetching library when you fetch data clientside. With it, you get the control over your clientside data back.

Then: set the staletime to a low value and it will invalidate when you use revalidatePath
Southern MartinOP
I got a Plan B: Go Server completely on this ShareModal 💀
It’s gonna be a Page, that i direct the user to
Answer
Southern MartinOP
^ this