Data Fetching Hooks
useCoreQuery
Type-safe hook for core-scoped queries (operations that don’t require a library).- Auto-generated types -
typeandinputare type-checked against Rust definitions - TanStack Query - Full TanStack Query API available
- Automatic caching - Query results are cached by key
- Type inference -
datatype is automatically inferred
useLibraryQuery
Type-safe hook for library-scoped queries (operations within a specific library).- Automatic library scoping - Uses current library ID from client
- Library switching - Automatically refetches when library changes
- Type safety - Input/output types inferred from operation
- Disabled when no library - Query is disabled if no library selected
useNormalizedCache
Event-driven cache updates for real-time sync across devices. See Normalized Cache for full documentation.- List queries that need instant updates across devices
- File listings that change frequently
- Real-time resource monitoring
Mutation Hooks
useCoreMutation
Type-safe hook for core-scoped mutations (operations that don’t require a library).- Type-safe input - Mutation input is type-checked
- Type-safe output - Success callback receives typed result
- Loading states -
isPending,isSuccess,isError - TanStack Query integration - Full mutation API available
useLibraryMutation
Type-safe hook for library-scoped mutations (operations within a specific library).- Automatic library scoping - Uses current library ID
- Type safety - Input/output types inferred
- Error handling - Throws if no library selected
- TanStack Query callbacks - onSuccess, onError, onSettled
Event Hooks
useEvent
Subscribe to specific Spacedrive events.- Event filtering - Only receives events of specified type
- Automatic cleanup - Unsubscribes on unmount
- Type-safe - Event type is checked at compile time
useAllEvents
Subscribe to all Spacedrive events (useful for debugging).useEvent for specific events in production.
Custom Hooks
useLibraries
Convenience hook for fetching all libraries.Client Hook
useSpacedriveClient
Access the Spacedrive client instance directly.client.setCurrentLibrary(id: string)- Switch to a libraryclient.getCurrentLibraryId()- Get current library IDclient.execute(method, input)- Execute RPC method directlyclient.on(event, handler)- Subscribe to eventsclient.off(event, handler)- Unsubscribe from events
Hook Patterns
Combining Queries
Dependent Queries
Mutations with Refetch
Optimistic Updates
Best Practices
Use the Right Hook
Set Appropriate Stale Times
Handle Loading and Error States
Invalidate Related Queries
Summary
Spacedrive’s React hooks provide:- Type safety - All operations are type-checked against Rust definitions
- Auto-generation - Types are generated from Rust, never manually written
- TanStack Query - Full TanStack Query API available
- Library scoping - Automatic library ID management
- Event subscriptions - Real-time updates via WebSocket
- Normalized cache - Instant cross-device sync
useCoreQuery and useLibraryQuery for data fetching, useCoreMutation and useLibraryMutation for mutations, and useEvent for event subscriptions.