can we pass value from client side to server side component
Unanswered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
can we pass the state values into the server component like this. for example if i have a button in cline side component but when i click the button the the state will change with the help of useState, based on the changed state i need some fetching inside my server component . is that possible for me to pass the data to the server component through this way
24 Replies
Original message was deleted
Dunker
You can wrap,
<ClientComp
<ServerComp
</ClientComp
Thus you can send data to the server component
<ClientComp
<ServerComp
</ClientComp
Thus you can send data to the server component
i don't think that will actually work @Dunker , i feel like OP should look into server actions (or maybe i am misunderstanding)
@riský i don't think that will actually work <@1017915332305879061> , i feel like OP should look into server actions (or maybe i am misunderstanding)
Dunker
Well no, data flow is from client to server via state
but server component in client component props should be static, as in can't actually send anything back?
@riský i don't think that will actually work <@1017915332305879061> , i feel like OP should look into server actions (or maybe i am misunderstanding)
Cuvier’s Dwarf Caiman
Yeah u can't wrap server components inside client components
@Cuvier’s Dwarf Caiman Yeah u can't wrap server components inside client components
you can because it is just the children prop which will be static
you just can't interact with it
Giant panda
A lot of misinformation in this thread. First of all, client and server components can be interleaved and you can place server components into client components, but you can not interactively pass client component data to server components without issuing a new request. For that you need to either trigger server actions or just pass the data to the server. A very common example are filtering and search described here: https://nextjs-faq.com/sharing-client-side-state-with-server-components
Dunker
I am actually wondering what could happen when you apply this pattern, cuz at docs it says server can be wrapped in client as any prop
'use client'
import { useState } from 'react'
export default function ClientComponent({
children,
}: {
children: React.ReactNode
}) {
const [count, setCount] = useState(0)
return (
<>
<button onClick={() => setCount(count + 1)}>{count}</button>
{children}
</>
)
}// This pattern works:
// You can pass a Server Component as a child or prop of a
// Client Component.
import ClientComponent from './client-component'
import ServerComponent from './server-component'
// Pages in Next.js are Server Components by default
export default function Page() {
return (
<ClientComponent>
<ServerComponent />
</ClientComponent>
)
}yes that is valid, and what Near said (as your not passing any data to server component there)
Dunker
When server is parent, we need client data to it. X
When client is parent, Server only rendered as static. X
When client is parent, Server only rendered as static. X
There is no way to solve this imo
Bad one, i am so often having this kinda issues
and iirc the link shows how you so something like this correctly
Dunker
Link says you need server as parent, but our client is parent
Maybe there is even no relationship
Actually i don't know the structure of app, cuz any change will affect it, as I am coming from pages dir, this looks like crucial to me
Giant panda
Please don't hijack the thread of someone else for your purposes.
Dunker
No no just to say I have similar issue
But okay I am leaving here
Asiatic LionOP
i didn't able pass the data through this way but the other way is dynamic routing . i made a seperate [value] route and each time i change state inside client i use a useRouter and pased to the [value] route and it is a serverside page and i was able to fetch the data based on the changes state in client.
Siberian Flycatcher
This is the way.
Move your client state to the URL. Then, use the URL to conditionally render server components.
Move your client state to the URL. Then, use the URL to conditionally render server components.