Skip to content

refactor: deprecates params and search params contexts#9581

Merged
jacobsfletch merged 3 commits into
mainfrom
chore/deprecate-contexts
Dec 2, 2024
Merged

refactor: deprecates params and search params contexts#9581
jacobsfletch merged 3 commits into
mainfrom
chore/deprecate-contexts

Conversation

@jacobsfletch

@jacobsfletch jacobsfletch commented Nov 27, 2024

Copy link
Copy Markdown
Member

As described in #9576, the SearchParamsProvider can become stale when navigating routes and relying on search params during initial render. This is because this context, along with the ParamsProvider, is duplicative to the internal lifecycle of useSearchParams and useParams from next/navigation– but always one render behind. Instead, we need to use the hooks directly from next/navigation as described in the jsdocs. This will also remove any abstraction over top the web standard for URLSearchParams.

For this reason, these providers and their corresponding hooks have been marked with the deprecated flag and will continue to behave as they do now, but will be removed in the next major release. This PR replaces all internal reliance on these hooks with next/navigation as suggested, except for the useParams hook, which was never used in the first place.

'use client'
- import { useSearchParams } from '@payloadcms/ui'
+ import { useSearchParams } from 'next/navigation'
+ import { parseSearchParams } from '@payloadcms/ui'

export function MyClientComponent() {
- const { searchParams } = useSearchParams()
+ const searchParams = useSearchParams()
+ const parsedParams = parseSearchParams(searchParams)

  // ...
}

MyClientComponent.tsx

Comment thread packages/ui/src/utilities/parseSearchParams.ts
@jacobsfletch jacobsfletch merged commit edc04ae into main Dec 2, 2024
@jacobsfletch jacobsfletch deleted the chore/deprecate-contexts branch December 2, 2024 18:58
@github-actions

github-actions Bot commented Dec 3, 2024

Copy link
Copy Markdown
Contributor

🚀 This is included in version v3.3.0

kendelljoseph pushed a commit that referenced this pull request Feb 21, 2025
As described in #9576, the `SearchParamsProvider` can become stale when
navigating routes and relying on search params during initial render.
This is because this context, along with the `ParamsProvider`, is
duplicative to the internal lifecycle of `useSearchParams` and
`useParams` from `next/navigation`– but always one render behind.
Instead, we need to use the hooks directly from `next/navigation` as
described in the jsdocs. This will also remove any abstraction over top
the web standard for `URLSearchParams`.

For this reason, these providers and their corresponding hooks have been
marked with the deprecated flag and will continue to behave as they do
now, but will be removed in the next major release. This PR replaces all
internal reliance on these hooks with `next/navigation` as suggested,
except for the `useParams` hook, which was never used in the first
place.

```diff
'use client'
- import { useSearchParams } from '@payloadcms/ui'
+ import { useSearchParams } from 'next/navigation'
+ import { parseSearchParams } from '@payloadcms/ui'

export function MyClientComponent() {
- const { searchParams } = useSearchParams()
+ const searchParams = useSearchParams()
+ const parsedParams = parseSearchParams(searchParams)

  // ...
}
```
_MyClientComponent.tsx_
jacobsfletch added a commit that referenced this pull request Jun 4, 2026
…er (#16884)

Removes the `ParamsProvider` and `SearchParamsProvider` from
`@payloadcms/ui`, which were deprecated in #9581 since
[v3.3.0](https://github.com/payloadcms/payload/releases/tag/v3.3.0).

The `useParams` and `useSearchParams` hooks remain available from
`@payloadcms/ui` with identical usage — they now originate from the new
[Router Adapter](#16763)
instead of these dedicated context providers. No migration steps are
needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants