Documentation Index
Fetch the complete documentation index at: https://mintlify.com/dip/cmdk/llms.txt
Use this file to discover all available pages before exploring further.
useCommandState lets you subscribe to the internal state of a Command menu and re-render only when the selected slice changes. It is built on useSyncExternalStore and is safe to use in concurrent React.
Pass a selector function that extracts the piece of state you need. The component re-renders only when the value returned by your selector changes — other state changes do not trigger a re-render.
This hook must be called inside a component that is rendered within a
<Command> tree. Calling it outside of a Command context will throw.Signature
A function that receives the full command menu state and returns the slice you want to subscribe to. The hook re-renders whenever the return value of this function changes (compared by reference equality).
State type
Thestate object passed to your selector has the following shape:
The current value of the search input. Updated synchronously as the user types.
The value of the currently selected item. Matches the
value prop (or inferred text content) of the focused Command.Item.The DOM
id attribute of the currently selected item element. Useful when you need to reference the selected element directly, for example to scroll it into view or measure it.The total number of items currently visible after filtering. This is
0 when no items match the search query and is the value Command.Empty uses to decide whether to render.A map from each visible item’s internal id to its filter score. Scores are numbers between
0 and 1, where 1 is the best match. Items with a score of 0 are hidden.The set of group ids that have at least one visible item. Groups not in this set are hidden when a search query is active.
Examples
Read the current search query
The most common use case is readingsearch inside a sub-component to render results that are fetched or computed outside the Command tree. This pattern is sometimes called a “SubItem”:
Read the currently selected value
Usevalue to highlight related UI outside the command menu, such as a preview panel:
Show a filtered result count
Usefiltered.count to display a badge showing how many items match the current query:
