Product Variant Hooks - Admin Components

Note: These hooks are available since Medusa v2.21.0.

The product variant hooks fetch product variants across all products from the Medusa backend. To fetch the variants of a single product, use useProductVariants instead.

Code
import {
  productVariantQueryKeys,
  useInfiniteVariants,
  useVariants,
} from "@medusajs/dashboard/hooks"

These hooks are built on TanStack Query. Use them in your widgets and UI routes.


useVariants#

Fetches a paginated list of product variants.

Code
const { variants, count } = useVariants({
  limit: 20,
})

Parameters#

Loading...

Returns#

Along with the following, the hook returns the rest of TanStack Query's useQuery result.

Loading...

useInfiniteVariants#

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.

Code
const { data, fetchNextPage, hasNextPage } =
  useInfiniteVariants({ limit: 20 })

const variants = data?.pages.flatMap(
  (page) => page.variants
)

Parameters#

Loading...

Returns#

Along with the following, the hook returns the rest of TanStack Query's useInfiniteQuery result.

Loading...

productVariantQueryKeys#

A TanStack Query key factory for product variant queries. It provides all, lists(), list(query), details(), and detail(id, query).

useInfiniteVariants appends "__infinite" to the list key, so its cache entries are separate from the ones of useVariants. Invalidating lists() invalidates both.

Code
import { useQueryClient } from "@tanstack/react-query"
import {
  productVariantQueryKeys,
} from "@medusajs/dashboard/hooks"

const Widget = () => {
  const queryClient = useQueryClient()

  const refreshVariants = () => {
    queryClient.invalidateQueries({
      queryKey: productVariantQueryKeys.lists(),
    })
  }

  // ...
}
Was this page helpful?
Ask Bloom
For assistance in your development, use Claude Code Plugins or Docs MCP server in Cursor, VSCode, etc...FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break
⇧↵