Skip to main content

Overview

useCollections is the primary hook for working with a user’s collection library. It automatically fetches the root collection on mount, loads sub-collections as you navigate deeper, and exposes actions for creating, updating, deleting, and saving entities.
Requires an authenticated user. The hook reads user and project context from the nearest SublayProvider.

Usage Example

Parameters

This hook accepts no required parameters. Pass an empty object or nothing.

Returns

Collection | null
The collection currently being viewed. Starts as the root collection on mount.
Collection[]
Direct child collections of currentCollection. Loaded automatically when currentCollection changes.
boolean
true while fetching the root collection or sub-collections.
(collection: Collection) => void
Navigate into a sub-collection. Pushes to the internal navigation stack and fetches its children.
() => void
Navigate to the previous collection in the stack.
() => void
Jump directly back to the root collection.
({ name }: CreateCollectionProps) => Promise<void>
Create a new sub-collection inside the current collection. name is trimmed and must be 1 to 255 characters after trimming.
({ collectionId, name }: UpdateCollectionProps) => Promise<void>
Rename a collection: the new name goes next to collectionId. The root collection cannot be renamed. Loaded entity lists that list the collection in saved show the new name.
({ collection }: DeleteCollectionProps) => Promise<{ deletedCollectionIds: string[] } | undefined>
Delete a collection and every collection under it. The root collection cannot be deleted. Once it succeeds, it resolves to { deletedCollectionIds } (the deleted collection and every collection under it). If the current collection is one of them, navigation moves back to the most recent collection in the history that wasn’t deleted; otherwise it stays where it is. Entity lists loaded with useEntityList that carry saved drop the deleted collections from each entity’s saved.collections; isSaved stays true only while another collection holds it. Resolves to undefined when the delete fails.
({ entity }: AddToCollectionProps) => Promise<void>
Save an entity (the whole Entity object) into the current collection. Loaded entity lists that carry saved (fetched with include=saved) show the save once it succeeds: the collection is added to the entity’s saved.collections and isSaved becomes true.
({ entityId }: RemoveFromCollectionProps) => Promise<void>
Remove an entity from the current collection. Loaded entity lists that carry saved drop the collection from the entity’s saved.collections once the removal succeeds, and isSaved stays true only while another collection holds it.
For a full integration guide, see Managing Collections.