|
1
|
--- |
|
2
|
sidebar_label: "useQueryParams" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `useQueryParams - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
This hook is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
The `useQueryParams` hook reads a set of query parameters from the current URL, with optional support for a prefix. The Medusa Admin uses it to build the list request of a [data table](!ui!/components/data-table) from the table's pagination, search, sorting, and filter state. |
|
20
|
|
|
21
|
```tsx |
|
22
|
import { useQueryParams } from "@medusajs/dashboard/hooks" |
|
23
|
``` |
|
24
|
|
|
25
|
--- |
|
26
|
|
|
27
|
## Example |
|
28
|
|
|
29
|
Pass the query parameters that you want to read, then use them in a request to the Medusa backend: |
|
30
|
|
|
31
|
```tsx |
|
32
|
import { useQueryParams } from "@medusajs/dashboard/hooks" |
|
33
|
import { useQuery } from "@tanstack/react-query" |
|
34
|
import { sdk } from "../lib/sdk" |
|
35
|
|
|
36
|
const PAGE_SIZE = 20 |
|
37
|
|
|
38
|
const ProductList = () => { |
|
39
|
const { offset, q, order } = useQueryParams([ |
|
40
|
"offset", |
|
41
|
"q", |
|
42
|
"order", |
|
43
|
]) |
|
44
|
|
|
45
|
const searchParams = { |
|
46
|
limit: PAGE_SIZE, |
|
47
|
offset: offset ? Number(offset) : 0, |
|
48
|
q, |
|
49
|
order, |
|
50
|
} |
|
51
|
|
|
52
|
const { data } = useQuery({ |
|
53
|
queryKey: ["products", searchParams], |
|
54
|
queryFn: () => sdk.admin.product.list(searchParams), |
|
55
|
}) |
|
56
|
|
|
57
|
// ... |
|
58
|
} |
|
59
|
``` |
|
60
|
|
|
61
|
If two tables are on the same page, pass a prefix as a second parameter so that each table reads its own query parameters. For example, `useQueryParams(["offset"], "sc")` reads the `sc_offset` query parameter and returns it as `offset`. |
|
62
|
|
|
63
|
Since every returned value is a string, convert the values that aren't strings. For example, use `Number(offset)` for numbers and `JSON.parse(created_at)` for filters whose values are objects. |
|
64
|
|
|
65
|
--- |
|
66
|
|
|
67
|
## Parameters |
|
68
|
|
|
69
|
<TypeList |
|
70
|
types={[ |
|
71
|
{ |
|
72
|
name: "keys", |
|
73
|
type: "`string[]`", |
|
74
|
optional: false, |
|
75
|
description: "Required. The names of the query parameters to read. They're also the keys of the returned object.", |
|
76
|
}, |
|
77
|
{ |
|
78
|
name: "prefix", |
|
79
|
type: "`string`", |
|
80
|
optional: false, |
|
81
|
description: "A prefix to prepend to every key when reading the URL, separated by an underscore. The returned object still uses the unprefixed keys.", |
|
82
|
}, |
|
83
|
]} |
|
84
|
/> |
|
85
|
|
|
86
|
--- |
|
87
|
|
|
88
|
## Returns |
|
89
|
|
|
90
|
Returns a `Record<string, string | undefined>` whose keys are the passed `keys`. Each value is the query parameter's value in the current URL, or `undefined` if the URL doesn't have it. |