Proposal: useState
-like hook for RSC to facilitate client<->server dataflow
#72554
Replies: 1 comment
-
I forgot to mention that I started this train of thought because importing server components into client components is unsupported. Indeed, there are open questions about how something like that would work. For example, how would we handle a client component trying to pass children to a server component? But the more I think about it, the more I think it would be better (from a DX perspective) to figure out answers to those questions and support that usage. I found #70289, which is essentially asking for that, so I am going to close this thread and continue the discussion there. I wanted to record some thoughts I had about how multiple |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Use server components for fetching data independent of the document URL.
Non-Goals
No response
Background
Server components are an awesome abstraction for fetching data corresponding to a document URL (path + query params). However, there are times when client-side interactivity requires fetching data independent of the document URL. Some examples:
It would be nice if we could leverage server components for data fetching in these cases, instead of doing a client-side
fetch
+ API dance.Proposal
A
useState
-like hook for server components where thesetState
function could be passed to client components and be "serialized" similar to a server action function.As an example:
Then, the
DataTable
client component could callsetSearchTerm
whenever an input changes. The call would send aGET
request with a special query param that encodes the new client state (the search term). The server component would rerender with a newsearchTerm
value thatuseClientState
extracts from the special query param. The new component tree would be sent to the client and reconciled while preserving client state. There could also be a built-in debouncing mechanism.Notes
An alternative could be a client router function that performs a navigation without changing the browser URL. Possibly limit the navigation to only changing query params for the current page. The server component would then extract the client state from the query params instead of using a hook.
However, that alternative feels less React-y and would be specific to Next. I would like the proposed feature to eventually be included in React proper. I am proposing it here because it seems like it would be easier to prototype in Next.
Beta Was this translation helpful? Give feedback.
All reactions