|
1
|
--- |
|
2
|
sidebar_label: "Store" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Store 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 store hooks retrieve the active store from the Medusa backend, including its supported currencies and default sales channel. |
|
20
|
|
|
21
|
```tsx |
|
22
|
import { |
|
23
|
storeQueryKeys, |
|
24
|
useStore, |
|
25
|
} from "@medusajs/dashboard/hooks" |
|
26
|
``` |
|
27
|
|
|
28
|
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). |
|
29
|
|
|
30
|
--- |
|
31
|
|
|
32
|
## useStore |
|
33
|
|
|
34
|
Retrieve the active store. The hook lists the stores and returns the first one, so it doesn't accept an ID. |
|
35
|
|
|
36
|
```tsx |
|
37
|
const { store, isPending } = useStore({ |
|
38
|
fields: "+supported_currencies.*", |
|
39
|
}) |
|
40
|
``` |
|
41
|
|
|
42
|
### Parameters |
|
43
|
|
|
44
|
<TypeList |
|
45
|
types={[ |
|
46
|
{ |
|
47
|
name: "query", |
|
48
|
type: "`SelectParams`", |
|
49
|
optional: true, |
|
50
|
description: "Optional selection parameters, such as `fields`.", |
|
51
|
}, |
|
52
|
{ |
|
53
|
name: "options", |
|
54
|
type: "`object`", |
|
55
|
optional: true, |
|
56
|
description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.", |
|
57
|
}, |
|
58
|
]} |
|
59
|
/> |
|
60
|
|
|
61
|
<Note type="warning"> |
|
62
|
|
|
63
|
The request throws a `404` error when the Medusa backend has no store. |
|
64
|
|
|
65
|
</Note> |
|
66
|
|
|
67
|
### Returns |
|
68
|
|
|
69
|
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. |
|
70
|
|
|
71
|
<TypeList |
|
72
|
types={[ |
|
73
|
{ |
|
74
|
name: "store", |
|
75
|
type: "`AdminStore`", |
|
76
|
description: "The active store. It's `undefined` until the request resolves.", |
|
77
|
}, |
|
78
|
]} |
|
79
|
/> |
|
80
|
|
|
81
|
--- |
|
82
|
|
|
83
|
## storeQueryKeys |
|
84
|
|
|
85
|
`storeQueryKeys` is the query key factory for store queries. `useStore` registers its data under `storeQueryKeys.details()`. |
|
86
|
|
|
87
|
Use it to invalidate the cached store after a custom mutation: |
|
88
|
|
|
89
|
```tsx |
|
90
|
import { useQueryClient } from "@tanstack/react-query" |
|
91
|
import { storeQueryKeys } from "@medusajs/dashboard/hooks" |
|
92
|
|
|
93
|
const queryClient = useQueryClient() |
|
94
|
|
|
95
|
queryClient.invalidateQueries({ |
|
96
|
queryKey: storeQueryKeys.details(), |
|
97
|
}) |
|
98
|
``` |