import * as React from "react"; import Code from "../Code"; export default function State() { return (
While it is great that kbar exposes some primitive components; e.g.{" "}
KBarSearch, KBarResults, etc., what if you
wanted to build some custom components, perhaps a set of breadcrumbs
that display the current action and it's ancestor actions?
useKBar enables you to hook into the current state of kbar
and collect the values you need to build your custom UI.
{
let actionAncestors = [];
const collectAncestors = (actionId) => {
const action = state.actions[actionId];
if (!action.parent) {
return null;
}
actionWithAncestors.unshift(action);
const parent = state.actions[action.parent];
collectAncestors(parent);
};
return {
actionAncestors
}
})
}
return (
{actionWithAncestors.map(action => (
-
// ...
))}
);`}
/>
Pass a callback to useKBar and retrieve only what you
collect. This pattern was introduced to me by my friend{" "}
Prev
. Reading any value from state enables you to create quite powerful
UIs – in fact, all of kbar's internal components are built using the
same pattern.