Replies: 1 comment 1 reply
-
The reason this doesn't exist is because if you used inside of a layout, then you'd need to refetch the layout upon every navigation.
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
searchParams
through multiple layers of nested Server Components.searchParams
directly, without their parents needing to be aware of this dependency.searchParams()
function that mirrors the convenience of existing functions likeheaders()
andcookies()
.Non-Goals
useSearchParams
hook for Client Components.searchParams
via this method would rightly opt the page into server-side dynamic rendering, consistent withheaders()
andcookies()
.Background
In the current version of the App Router, the only way for a Server Component to access URL search parameters is to receive them as props passed down from a root component like
page.tsx
. This is the only alternative.This requirement leads to a common and cumbersome pattern known as "prop drilling." If a deeply nested component (e.g., a
<Pagination />
component) needs thepage
parameter, every single component in its parent hierarchy must acceptsearchParams
as a prop and forward it.Example of the current alternative (prop drilling):
This pattern creates unnecessary boilerplate and tightly couples components.
The need for this feature is highlighted by the existence of prior art within Next.js itself: the
headers()
andcookies()
functions fromnext/headers
. These functions set a clear precedent for providing convenient, direct access to request-specific data in any Server Component without prop drilling. This proposal aims to extend that same ergonomic developer experience tosearchParams
.A similar issue was already mentioned for
params
in another discussion here #45543Proposal
This feature should be implemented as a new function,
searchParams()
, exported fromnext/navigation
.When called within a Server Component, it would return a read-only instance of
URLSearchParams
, allowing developers to easily access the query string parameters of the current request.Proposed Implementation:
A new function would be exposed, allowing any Server Component to be refactored like this:
This implementation would make the component self-contained and remove the need for any parent component to know about or pass down
searchParams
.I believe this is a crucial quality-of-life improvement for the framework.
Beta Was this translation helpful? Give feedback.
All reactions