|
1
|
--- |
|
2
|
sidebar_label: "Customers" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Customer 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 customer hooks fetch customers and their addresses from the Medusa backend. Use them to show customer data in your admin customizations. |
|
20
|
|
|
21
|
```tsx |
|
22
|
import { |
|
23
|
customersQueryKeys, |
|
24
|
useCustomer, |
|
25
|
useCustomerAddress, |
|
26
|
useCustomers, |
|
27
|
useListCustomerAddresses, |
|
28
|
} from "@medusajs/dashboard/hooks" |
|
29
|
``` |
|
30
|
|
|
31
|
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). |
|
32
|
|
|
33
|
--- |
|
34
|
|
|
35
|
## useCustomers |
|
36
|
|
|
37
|
Fetches a paginated list of customers. Pass query parameters to filter, paginate, or select the fields of the returned customers. |
|
38
|
|
|
39
|
```tsx |
|
40
|
const { customers, count, isPending } = useCustomers({ |
|
41
|
limit: 20, |
|
42
|
}) |
|
43
|
``` |
|
44
|
|
|
45
|
### Parameters |
|
46
|
|
|
47
|
<TypeList |
|
48
|
types={[ |
|
49
|
{ |
|
50
|
name: "query", |
|
51
|
type: "`Record<string, any>`", |
|
52
|
optional: true, |
|
53
|
description: "Optional query parameters to pass to the list customers API route, such as `limit`, `offset`, or `fields`.", |
|
54
|
}, |
|
55
|
{ |
|
56
|
name: "options", |
|
57
|
type: "`UseQueryOptions`", |
|
58
|
optional: true, |
|
59
|
description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.", |
|
60
|
}, |
|
61
|
]} |
|
62
|
/> |
|
63
|
|
|
64
|
### Returns |
|
65
|
|
|
66
|
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. |
|
67
|
|
|
68
|
<TypeList |
|
69
|
types={[ |
|
70
|
{ |
|
71
|
name: "customers", |
|
72
|
type: "`AdminCustomer[]`", |
|
73
|
description: "The list of customers. It's `undefined` while the request is loading.", |
|
74
|
}, |
|
75
|
{ |
|
76
|
name: "count", |
|
77
|
type: "`number`", |
|
78
|
description: "The total number of customers matching the query.", |
|
79
|
}, |
|
80
|
{ |
|
81
|
name: "limit", |
|
82
|
type: "`number`", |
|
83
|
description: "The maximum number of customers returned in the request.", |
|
84
|
}, |
|
85
|
{ |
|
86
|
name: "offset", |
|
87
|
type: "`number`", |
|
88
|
description: "The number of customers skipped before the returned ones.", |
|
89
|
}, |
|
90
|
]} |
|
91
|
/> |
|
92
|
|
|
93
|
--- |
|
94
|
|
|
95
|
## useCustomer |
|
96
|
|
|
97
|
Fetches a single customer by ID. |
|
98
|
|
|
99
|
```tsx |
|
100
|
const { customer, isPending } = useCustomer(customerId, { |
|
101
|
fields: "+groups.id", |
|
102
|
}) |
|
103
|
``` |
|
104
|
|
|
105
|
### Parameters |
|
106
|
|
|
107
|
<TypeList |
|
108
|
types={[ |
|
109
|
{ |
|
110
|
name: "id", |
|
111
|
type: "`string`", |
|
112
|
optional: false, |
|
113
|
description: "The customer's ID.", |
|
114
|
}, |
|
115
|
{ |
|
116
|
name: "query", |
|
117
|
type: "`Record<string, any>`", |
|
118
|
optional: true, |
|
119
|
description: "Optional query parameters, such as `fields`.", |
|
120
|
}, |
|
121
|
{ |
|
122
|
name: "options", |
|
123
|
type: "`UseQueryOptions`", |
|
124
|
optional: true, |
|
125
|
description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.", |
|
126
|
}, |
|
127
|
]} |
|
128
|
/> |
|
129
|
|
|
130
|
### Returns |
|
131
|
|
|
132
|
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. |
|
133
|
|
|
134
|
<TypeList |
|
135
|
types={[ |
|
136
|
{ |
|
137
|
name: "customer", |
|
138
|
type: "`AdminCustomer`", |
|
139
|
description: "The customer's details. It's `undefined` while the request is loading.", |
|
140
|
}, |
|
141
|
]} |
|
142
|
/> |
|
143
|
|
|
144
|
--- |
|
145
|
|
|
146
|
## useListCustomerAddresses |
|
147
|
|
|
148
|
Fetches the list of addresses of a customer. |
|
149
|
|
|
150
|
```tsx |
|
151
|
const { addresses, count } = |
|
152
|
useListCustomerAddresses(customerId) |
|
153
|
``` |
|
154
|
|
|
155
|
### Parameters |
|
156
|
|
|
157
|
<TypeList |
|
158
|
types={[ |
|
159
|
{ |
|
160
|
name: "id", |
|
161
|
type: "`string`", |
|
162
|
optional: false, |
|
163
|
description: "The customer's ID.", |
|
164
|
}, |
|
165
|
{ |
|
166
|
name: "options", |
|
167
|
type: "`UseQueryOptions`", |
|
168
|
optional: true, |
|
169
|
description: "Optional TanStack Query options. This hook accepts the options as its second parameter, not a query object.", |
|
170
|
}, |
|
171
|
]} |
|
172
|
/> |
|
173
|
|
|
174
|
### Returns |
|
175
|
|
|
176
|
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. |
|
177
|
|
|
178
|
<TypeList |
|
179
|
types={[ |
|
180
|
{ |
|
181
|
name: "addresses", |
|
182
|
type: "`AdminCustomerAddress[]`", |
|
183
|
description: "The customer's addresses. It's `undefined` while the request is loading.", |
|
184
|
}, |
|
185
|
{ |
|
186
|
name: "count", |
|
187
|
type: "`number`", |
|
188
|
description: "The total number of the customer's addresses.", |
|
189
|
}, |
|
190
|
{ |
|
191
|
name: "limit", |
|
192
|
type: "`number`", |
|
193
|
description: "The maximum number of addresses returned in the request.", |
|
194
|
}, |
|
195
|
{ |
|
196
|
name: "offset", |
|
197
|
type: "`number`", |
|
198
|
description: "The number of addresses skipped before the returned ones.", |
|
199
|
}, |
|
200
|
]} |
|
201
|
/> |
|
202
|
|
|
203
|
--- |
|
204
|
|
|
205
|
## useCustomerAddress |
|
206
|
|
|
207
|
Fetches a single address of a customer. |
|
208
|
|
|
209
|
```tsx |
|
210
|
const { address } = useCustomerAddress( |
|
211
|
customerId, |
|
212
|
addressId |
|
213
|
) |
|
214
|
``` |
|
215
|
|
|
216
|
### Parameters |
|
217
|
|
|
218
|
<TypeList |
|
219
|
types={[ |
|
220
|
{ |
|
221
|
name: "id", |
|
222
|
type: "`string`", |
|
223
|
optional: false, |
|
224
|
description: "The customer's ID.", |
|
225
|
}, |
|
226
|
{ |
|
227
|
name: "addressId", |
|
228
|
type: "`string`", |
|
229
|
optional: false, |
|
230
|
description: "The address's ID.", |
|
231
|
}, |
|
232
|
{ |
|
233
|
name: "options", |
|
234
|
type: "`UseQueryOptions`", |
|
235
|
optional: true, |
|
236
|
description: "Optional TanStack Query options.", |
|
237
|
}, |
|
238
|
]} |
|
239
|
/> |
|
240
|
|
|
241
|
### Returns |
|
242
|
|
|
243
|
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. |
|
244
|
|
|
245
|
<TypeList |
|
246
|
types={[ |
|
247
|
{ |
|
248
|
name: "address", |
|
249
|
type: "`AdminCustomerAddress`", |
|
250
|
description: "The address's details. It's `undefined` while the request is loading.", |
|
251
|
}, |
|
252
|
]} |
|
253
|
/> |
|
254
|
|
|
255
|
--- |
|
256
|
|
|
257
|
## customersQueryKeys |
|
258
|
|
|
259
|
A TanStack Query key factory for customer queries. Use it to invalidate cached customer data after you change a customer. |
|
260
|
|
|
261
|
It provides `all`, `lists()`, `list(query)`, `details()`, and `detail(id, query)`. |
|
262
|
|
|
263
|
```tsx |
|
264
|
import { useQueryClient } from "@tanstack/react-query" |
|
265
|
import { |
|
266
|
customersQueryKeys, |
|
267
|
} from "@medusajs/dashboard/hooks" |
|
268
|
|
|
269
|
const Widget = () => { |
|
270
|
const queryClient = useQueryClient() |
|
271
|
|
|
272
|
const refreshCustomer = (id: string) => { |
|
273
|
queryClient.invalidateQueries({ |
|
274
|
queryKey: customersQueryKeys.detail(id), |
|
275
|
}) |
|
276
|
} |
|
277
|
|
|
278
|
// ... |
|
279
|
} |
|
280
|
``` |