useDebouncedSearch - Admin Components
Note: This hook is available since Medusa v2.21.0.
The useDebouncedSearch hook manages a search input's value and exposes a debounced copy of it. The Medusa Admin uses it to avoid sending a request to the Medusa backend on every keystroke, such as in the useComboboxData hook.
Example#
Bind searchValue to the input and use query in the request to the Medusa backend:
import { useDebouncedSearch } from "@medusajs/dashboard/hooks"
import { Input } from "@medusajs/ui"
import { useQuery } from "@tanstack/react-query"
import { sdk } from "../lib/sdk"
const ProductSearch = () => {
const { searchValue, onSearchValueChange, query } =
useDebouncedSearch()
const { data } = useQuery({
queryKey: ["products", query],
queryFn: () => sdk.admin.product.list({ q: query }),
})
return (
<Input
value={searchValue}
onChange={(e) => onSearchValueChange(e.target.value)}
placeholder="Search products"
/>
)
}
The hook updates query 300 milliseconds after the user stops typing, so the query only refetches once for a series of keystrokes.
Returns#
Loading...
Was this page helpful?