LoopKitFrontend
Deck Hooks
useDeck, useDecks, and useDeckTree hooks
useDeck
Fetches a single deck with its card counts.
import { useDeck } from '@loopkit/react';
function DeckPage({ id }: { id: string }) {
const { deck, counts, loading, error, refetch } = useDeck(id);
}Return Type
interface UseDeckReturn {
deck: DeckBase | null;
counts: DeckCounts | null;
loading: boolean;
error: string | null;
refetch: () => Promise<void>;
}| Property | Type | Description |
|---|---|---|
deck | DeckBase | null | Deck data |
counts | DeckCounts | null | Card counts { new, learning, review, relearning, total } |
loading | boolean | Loading state |
error | string | null | Error message |
refetch | () => Promise<void> | Manually refetch deck data |
useDecks
Fetches all decks as a flat list.
import { useDecks } from '@loopkit/react';
function DeckListPage() {
const { decks, loading, error, refetch } = useDecks();
}Return Type
interface UseDecksReturn {
decks: DeckBase[];
loading: boolean;
error: string | null;
refetch: () => Promise<void>;
}useDeckTree
Fetches the full deck hierarchy as a tree.
import { useDeckTree } from '@loopkit/react';
function DeckBrowser() {
const { tree, loading, error, refetch } = useDeckTree();
}Return Type
interface UseDeckTreeReturn {
tree: DeckTreeNode[];
loading: boolean;
error: string | null;
refetch: () => Promise<void>;
}interface DeckTreeNode extends DeckBase {
children: DeckTreeNode[];
counts?: DeckCounts;
}Example: Recursive Deck Tree
function DeckNode({ node }: { node: DeckTreeNode }) {
return (
<div style={{ paddingLeft: '1rem' }}>
<div>
<strong>{node.name}</strong>
{node.counts && (
<span>
{' '}— {node.counts.new} new, {node.counts.review} review
</span>
)}
</div>
{node.children.map((child) => (
<DeckNode key={child.id} node={child} />
))}
</div>
);
}
function DeckBrowser() {
const { tree, loading } = useDeckTree();
if (loading) return <p>Loading...</p>;
return (
<div>
{tree.map((root) => (
<DeckNode key={root.id} node={root} />
))}
</div>
);
}