Raw
1 ---
2 sidebar_label: "Regions"
3 ---
4
5 import { TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `Region Hooks - Admin Components`,
9 }
10
11 # {metadata.title}
12
13 <Note>
14
15 These hooks are available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0).
16
17 </Note>
18
19 The region hooks fetch regions from the Medusa backend. Use them to show or select regions in your admin customizations.
20
21 ```tsx
22 import {
23 regionsQueryKeys,
24 useRegion,
25 useRegions,
26 } from "@medusajs/dashboard/hooks"
27 ```
28
29 These hooks are built on [TanStack Query](https://tanstack.com/query/latest). Use them in your [widgets](!docs!/learn/fundamentals/admin/widgets) and [UI routes](!docs!/learn/fundamentals/admin/ui-routes).
30
31 ---
32
33 ## useRegion
34
35 Fetches a single region by ID.
36
37 ```tsx
38 const { region, isPending } = useRegion(regionId, {
39 fields: "+countries.*",
40 })
41 ```
42
43 ### Parameters
44
45 <TypeList
46 types={[
47 {
48 name: "id",
49 type: "`string`",
50 optional: false,
51 description: "The region's ID.",
52 },
53 {
54 name: "query",
55 type: "`Record<string, any>`",
56 optional: true,
57 description: "Optional query parameters, such as `fields`.",
58 },
59 {
60 name: "options",
61 type: "`UseQueryOptions`",
62 optional: true,
63 description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.",
64 },
65 ]}
66 />
67
68 ### Returns
69
70 Along with the following, the hook returns the rest of TanStack Query's [`useQuery`](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery) result.
71
72 <TypeList
73 types={[
74 {
75 name: "region",
76 type: "`AdminRegion`",
77 description: "The region's details. It's `undefined` while the request is loading.",
78 },
79 ]}
80 />
81
82 ---
83
84 ## useRegions
85
86 Fetches a paginated list of regions.
87
88 ```tsx
89 const { regions, count } = useRegions({ limit: 20 })
90 ```
91
92 ### Parameters
93
94 <TypeList
95 types={[
96 {
97 name: "query",
98 type: "`Record<string, any>`",
99 optional: true,
100 description: "Optional query parameters, such as `limit`, `offset`, or `fields`.",
101 },
102 {
103 name: "options",
104 type: "`UseQueryOptions`",
105 optional: true,
106 description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.",
107 },
108 ]}
109 />
110
111 ### Returns
112
113 Along with the following, the hook returns the rest of TanStack Query's [`useQuery`](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery) result.
114
115 <TypeList
116 types={[
117 {
118 name: "regions",
119 type: "`AdminRegion[]`",
120 description: "The list of regions. It's `undefined` while the request is loading.",
121 },
122 {
123 name: "count",
124 type: "`number`",
125 description: "The total number of regions matching the query.",
126 },
127 {
128 name: "limit",
129 type: "`number`",
130 description: "The maximum number of regions returned in the request.",
131 },
132 {
133 name: "offset",
134 type: "`number`",
135 description: "The number of regions skipped before the returned ones.",
136 },
137 ]}
138 />
139
140 ---
141
142 ## regionsQueryKeys
143
144 A TanStack Query key factory for region queries. Use it to invalidate cached region data after you change a region.
145
146 It provides `all`, `lists()`, `list(query)`, `details()`, and `detail(id, query)`.
147
148 ```tsx
149 import { useQueryClient } from "@tanstack/react-query"
150 import { regionsQueryKeys } from "@medusajs/dashboard/hooks"
151
152 const Widget = () => {
153 const queryClient = useQueryClient()
154
155 const refreshRegions = () => {
156 queryClient.invalidateQueries({
157 queryKey: regionsQueryKeys.lists(),
158 })
159 }
160
161 // ...
162 }
163 ```