My App
RoomKitFrontend

useLocationTree

Fetch the hierarchical location tree

Overview

useLocationTree fetches the location hierarchy as a tree. It calls GET {apiUrl}/locations/tree.

import { useLocationTree } from '@roomkit/react';

Usage

function LocationPicker() {
    const { data, isLoading, error } = useLocationTree({
        scope: 'campus-a',  // optional: scope to a specific subtree
    });

    if (isLoading) return <div>Loading locations...</div>;

    return (
        <ul>
            {data?.map((node) => (
                <LocationTreeItem key={node.id} node={node} />
            ))}
        </ul>
    );
}

function LocationTreeItem({ node }: { node: LocationTreeNode }) {
    return (
        <li>
            {node.displayName} ({node.type})
            {node.children.length > 0 && (
                <ul>
                    {node.children.map((child) => (
                        <LocationTreeItem key={child.id} node={child} />
                    ))}
                </ul>
            )}
        </li>
    );
}

UseLocationTreeOptions

interface LocationTreeNode extends LocationNode {
    children: LocationTreeNode[];
}

interface UseLocationTreeOptions {
    scope?: string;
    enabled?: boolean;
    onSuccess?: (data: LocationTreeNode[]) => void;
    onError?: (error: Error) => void;
}
OptionTypeRequiredDescription
scopestringNoLocation node ID to scope the tree to a subtree
enabledbooleanNoSet to false to skip the request
onSuccess(data) => voidNoCalled when the request succeeds
onError(error) => voidNoCalled when the request fails

Return Value

FieldTypeDescription
dataLocationTreeNode[] | nullTree of location nodes
isLoadingbooleantrue while the request is in flight
errorError | nullThe error if the request failed
refetch() => Promise<void>Manually re-fetch the data

API Endpoint

GET {apiUrl}/locations/tree?scope=...

On this page