1 ---
2 sidebar_label: "Product Variants"
3 ---
4
5 import { TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `Product Variant 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 product variant hooks fetch product variants across all products from the Medusa backend. To fetch the variants of a single product, use [useProductVariants](../products/page.mdx#useproductvariants) instead.
20
21 ```tsx
22 import {
23 productVariantQueryKeys,
24 useInfiniteVariants,
25 useVariants,
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 ## useVariants
34
35 Fetches a paginated list of product variants.
36
37 ```tsx
38 const { variants, count } = useVariants({
39 limit: 20,
40 })
41 ```
42
43 ### Parameters
44
45 <TypeList
46 types={[
47 {
48 name: "query",
49 type: "`AdminProductVariantParams`",
50 optional: true,
51 description: "Optional query parameters, such as `limit`, `offset`, or `fields`.",
52 },
53 {
54 name: "options",
55 type: "`UseQueryOptions`",
56 optional: true,
57 description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.",
58 },
59 ]}
60 />
61
62 ### Returns
63
64 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.
65
66 <TypeList
67 types={[
68 {
69 name: "variants",
70 type: "`AdminProductVariant[]`",
71 description: "The list of product variants. It's `undefined` while the request is loading.",
72 },
73 {
74 name: "count",
75 type: "`number`",
76 description: "The total number of variants matching the query.",
77 },
78 {
79 name: "limit",
80 type: "`number`",
81 description: "The maximum number of variants returned in the request.",
82 },
83 {
84 name: "offset",
85 type: "`number`",
86 description: "The number of variants skipped before the returned ones.",
87 },
88 ]}
89 />
90
91 ---
92
93 ## useInfiniteVariants
94
95 Fetches product variants page by page, which is useful for infinite scrolling or "load more" lists. The hook manages the `offset` internally and defaults `limit` to `50`.
96
97 ```tsx
98 const { data, fetchNextPage, hasNextPage } =
99 useInfiniteVariants({ limit: 20 })
100
101 const variants = data?.pages.flatMap(
102 (page) => page.variants
103 )
104 ```
105
106 ### Parameters
107
108 <TypeList
109 types={[
110 {
111 name: "query",
112 type: `\`Omit<AdminProductVariantParams, "offset" | "limit"> & { limit?: number }\``,
113 optional: true,
114 description: "Optional query parameters. The hook sets the `offset` for each page, so you can only control the page size with `limit`.",
115 },
116 {
117 name: "options",
118 type: "`UseInfiniteQueryOptions`",
119 optional: true,
120 description: "Optional TanStack infinite query options, excluding `queryKey`, `queryFn`, `initialPageParam`, and `getNextPageParam`.",
121 },
122 ]}
123 />
124
125 ### Returns
126
127 Along with the following, the hook returns the rest of TanStack Query's [`useInfiniteQuery`](https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery) result.
128
129 <TypeList
130 types={[
131 {
132 name: "data",
133 type: "`InfiniteData<AdminProductVariantListResponse>`",
134 description: "The fetched pages. Each page in `data.pages` has `variants`, `count`, `limit`, and `offset`.",
135 },
136 {
137 name: "fetchNextPage",
138 type: "`() => Promise`",
139 description: "Fetches the next page of variants.",
140 },
141 {
142 name: "hasNextPage",
143 type: "`boolean`",
144 description: "Whether more variants are available to fetch.",
145 },
146 ]}
147 />
148
149 ---
150
151 ## productVariantQueryKeys
152
153 A TanStack Query key factory for product variant queries. It provides `all`, `lists()`, `list(query)`, `details()`, and `detail(id, query)`.
154
155 `useInfiniteVariants` appends `"__infinite"` to the list key, so its cache entries are separate from the ones of `useVariants`. Invalidating `lists()` invalidates both.
156
157 ```tsx
158 import { useQueryClient } from "@tanstack/react-query"
159 import {
160 productVariantQueryKeys,
161 } from "@medusajs/dashboard/hooks"
162
163 const Widget = () => {
164 const queryClient = useQueryClient()
165
166 const refreshVariants = () => {
167 queryClient.invalidateQueries({
168 queryKey: productVariantQueryKeys.lists(),
169 })
170 }
171
172 // ...
173 }
174 ```