-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40cb762
commit 7f93868
Showing
2 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,62 @@ | ||
--- | ||
title: useSearchParams | ||
toc: false | ||
--- | ||
|
||
# `useSearchParams` | ||
|
||
<docs-info>This hook is simply a re-export of [React Router's `useSearchParams`][rr-usesearchparams].</docs-info> | ||
Returns a tuple of the current [search params][searchparams] and a function to update them. Setting the search params causes a navigation. | ||
|
||
[rr-usesearchparams]: https://reactrouter.com/hooks/use-search-params | ||
```tsx | ||
import { useSearchParams } from "@remix-run/react"; | ||
|
||
export function SomeComponent() { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
// ... | ||
} | ||
``` | ||
|
||
## Signature | ||
|
||
<!-- eslint-disable --> | ||
|
||
```tsx | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
``` | ||
|
||
### `searchParams` | ||
|
||
The first value returned is a [Web URLSearchParams][searchparams] object. | ||
|
||
### `setSearchParams(params)` | ||
|
||
The second value returned is a function to set new search params and causes a navigation when called. | ||
|
||
```tsx | ||
<button | ||
onClick={() => { | ||
const params = new URLSearchParams(); | ||
params.set("someKey", "someValue"); | ||
setSearchParams(params); | ||
}} | ||
/> | ||
``` | ||
|
||
### `setSearchParams((prevParams) => newParams)` | ||
|
||
The setter function also supports a function for setting new search params. | ||
|
||
```tsx | ||
<button | ||
onClick={() => { | ||
setSearchParams((prev) => { | ||
prev.set("someKey", "someValue"); | ||
return prev; | ||
}); | ||
}} | ||
/> | ||
``` | ||
|
||
[searchparams]: https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams | ||
[usesearchparams-native]: ./use-search-params-rn | ||
[usestate]: https://react.dev/reference/react/useState | ||
[usenavigate]: ./use-navigate |