|
1 | 1 | --- |
2 | 2 | title: useSearchParams |
3 | | -use_cases: 'pagination, filters, search forms, url state management, query string updates' |
| 3 | +use_cases: "pagination, filters, search forms, url state management, query string updates" |
4 | 4 | tags: |
5 | 5 | - search |
6 | 6 | - query |
7 | 7 | - params |
8 | 8 | - pagination |
9 | 9 | - filters |
10 | 10 | - url |
11 | | -version: '1.0' |
| 11 | +version: "1.0" |
12 | 12 | description: >- |
13 | 13 | Manage URL query parameters with useSearchParams - handle pagination, filters, |
14 | 14 | and search state directly in the URL query string. |
15 | 15 | --- |
16 | 16 |
|
17 | | -Retrieves a tuple containing a reactive object to read the current location's query parameters and a method to update them. |
18 | | -The object is a proxy so you must access properties to subscribe to reactive updates. |
19 | | -Note values will be strings and property names will retain their casing. |
20 | | - |
21 | | -The setter method accepts an object as an input, and its key-value pairs will be merged into the existing query string. |
22 | | -If a value is `''`, `undefined` or `null`, the corresponding key will be omitted from the resulting query string. |
23 | | -The updates behave like navigation and will not scroll the page to the top. |
24 | | -Additionally, the setter can take an optional second parameter, the same as `navigate`, to control the navigation behavior and auto-scrolling, which are disabled by default. |
25 | | - |
26 | | -```js |
27 | | -const [searchParams, setSearchParams] = useSearchParams(); |
28 | | - |
29 | | -return ( |
30 | | - <div> |
31 | | - <span>Page: {searchParams.page}</span> |
32 | | - <button |
33 | | - onClick={() => |
34 | | - setSearchParams({ page: (parseInt(searchParams.page) || 0) + 1 }) |
35 | | - } |
36 | | - > |
37 | | - Next Page |
38 | | - </button> |
39 | | - </div> |
40 | | -); |
| 17 | +The `useSearchParams` function reads the URL query parameters for the current route and provides a function to update them. |
| 18 | + |
| 19 | +## Import |
| 20 | + |
| 21 | +```ts |
| 22 | +import { useSearchParams } from "@solidjs/router"; |
| 23 | +``` |
| 24 | + |
| 25 | +## Type |
| 26 | + |
| 27 | +```ts |
| 28 | +function useSearchParams<T extends Record<string, string | string[]>>(): [ |
| 29 | + Partial<T>, |
| 30 | + (params: SetSearchParams, options?: Partial<NavigateOptions>) => void, |
| 31 | +]; |
41 | 32 | ``` |
| 33 | + |
| 34 | +## Parameters |
| 35 | + |
| 36 | +`useSearchParams` takes no arguments. |
| 37 | + |
| 38 | +## Return value |
| 39 | + |
| 40 | +- **Type:** `[ Partial<T>, (params: SetSearchParams, options?: Partial<NavigateOptions>) => void ]` |
| 41 | + |
| 42 | +`useSearchParams` returns an array with two items. |
| 43 | + |
| 44 | +The first item is a reactive object containing the current query parameters. |
| 45 | +Accessing a property within a tracking scope registers a dependency, causing the computation to re-run when the parameter changes. |
| 46 | +Values are always strings. |
| 47 | + |
| 48 | +The second item is a function that updates the query string. |
| 49 | +It merges the object provided as its first argument with the current query parameters. |
| 50 | +Passing an empty string (`""`), an empty array (`[]`), `undefined`, or `null` as a value removes the key. |
| 51 | +It accepts the same options as [`useNavigate`](/solid-router/reference/primitives/use-navigate) as the second parameter. |
| 52 | +By default, the `resolve` and `scroll` options are set to `false`. |
| 53 | + |
| 54 | +## Examples |
| 55 | + |
| 56 | +### Basic usage |
| 57 | + |
| 58 | +```tsx |
| 59 | +import { useSearchParams } from "@solidjs/router"; |
| 60 | + |
| 61 | +function Paginator() { |
| 62 | + const [params, setParams] = useSearchParams(); |
| 63 | + |
| 64 | + const page = () => Number(params.page || "1"); |
| 65 | + |
| 66 | + return ( |
| 67 | + <div> |
| 68 | + <span>Current Page: {page()}</span> |
| 69 | + <button onClick={() => setParams({ page: page() + 1 })}>Next</button> |
| 70 | + </div> |
| 71 | + ); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## Related |
| 76 | + |
| 77 | +- [`useParams`](/solid-router/reference/primitives/use-params) |
| 78 | +- [`useLocation`](/solid-router/reference/primitives/use-location) |
| 79 | +- [`useNavigate`](/solid-router/reference/primitives/use-navigate) |
0 commit comments