Product Hooks - Admin Components
The product hooks fetch and mutate products and their variants in the Medusa backend. To fetch variants across all products, use the product variant hooks instead.
These hooks are built on TanStack Query. Use them in your widgets and UI routes.
useProducts#
Fetches a paginated list of products.
Parameters#
Returns
Along with the following, the hook returns the rest of TanStack Query's useQuery result.
useProduct#
Fetches a single product by ID.
Parameters#
Returns#
Along with the following, the hook returns the rest of TanStack Query's useQuery result.
useProductVariants#
Fetches the variants of a specific product. The first parameter is the product's ID, not a query object.
Parameters
Returns#
Along with the following, the hook returns the rest of TanStack Query's useQuery result.
useCreateProduct#
Creates a product. On success, the hook invalidates the product lists, along with the inventory item and product option lists, since creating a product can create those resources too.
Parameters#
Returns#
Along with the following, the hook returns the rest of TanStack Query's useMutation result.
useUpdateProduct#
Updates a product. The hook requests the updated product without its relations, so the response holds the product's own fields only. On success, it invalidates the product lists and the product's detail query.
Parameters#
Returns#
Along with the following, the hook returns the rest of TanStack Query's useMutation result.
useDeleteProduct#
Deletes a product. On success, the hook invalidates the product lists, the product's detail query, and the product option lists.
Parameters#
Returns#
Along with the following, the hook returns the rest of TanStack Query's useMutation result.
useUpdateProductVariantsBatch#
Updates multiple variants of a product in one request. The payload is the array of variant updates, and the hook wraps it in the batch request's update field for you.
Parameters#
Returns#
Along with the following, the hook returns the rest of TanStack Query's useMutation result.
useDeleteVariantLazy#
Deletes a variant of a product. The hook is bound to the product only, so you pass the variant's ID when you call the mutation. That makes it useful when one component deletes any of a product's variants.
Parameters#
Returns#
Along with the following, the hook returns the rest of TanStack Query's result.
productsQueryKeys#
A TanStack Query key factory for product queries. Use it to invalidate cached product data after you change a product.
It provides all, lists(), list(query), details(), and detail(id, query).
1import { useQueryClient } from "@tanstack/react-query"2import {3 productsQueryKeys,4} from "@medusajs/dashboard/hooks"5 6const Widget = () => {7 const queryClient = useQueryClient()8 9 const refreshProduct = (id: string) => {10 queryClient.invalidateQueries({11 queryKey: productsQueryKeys.detail(id),12 })13 }14 15 // ...16}
variantsQueryKeys#
A TanStack Query key factory for the variants of a product, which is the factory that useProductVariants uses. It's separate from productVariantQueryKeys, which the product variant hooks use.
1import { useQueryClient } from "@tanstack/react-query"2import {3 variantsQueryKeys,4} from "@medusajs/dashboard/hooks"5 6const Widget = () => {7 const queryClient = useQueryClient()8 9 const refreshVariants = () => {10 queryClient.invalidateQueries({11 queryKey: variantsQueryKeys.lists(),12 })13 }14 15 // ...16}