4

In Next 13 with React Server Components we can fetch data directly in our Components. but how to refetch that data when the state changes in the client component.

enter image description here

For example in Tree Structure Above. Suppose we have some data in Navbar (server-component). What if we want to fetch that data again when state changes in Search Component (client).

We can lift the state up or use context. but how will we implement useEffect like functionality. like when that state change in search component make server component to re render and fetch that data again with new params.

PS : I am trying to learn server component. and it's difficult to wrap head around new React Model & Architecture.

Muhammad Bilal
  • 113
  • 2
  • 8
  • Since, React Server Component does not work in client side, on state change you can call the API again using useEffect hook to fetch the fresh data again and re render the component. – Aamir Khan May 18 '23 at 07:15
  • @AamirKhan that's seem pretty dumb, as one would have to write the fetch logic twice but I don't know, getting frustrated with these RSC – Jumper Jul 28 '23 at 04:24
  • @Jumper I think the best way would be to move these states to the query params. and whenever those changes just revalidate the route and it will fetch data with the new params – Muhammad Bilal Aug 02 '23 at 06:05

0 Answers0