Skip to main content
Spacedrive provides type-safe React hooks for data fetching, mutations, and event subscriptions. All types are auto-generated from Rust definitions.

Data Fetching Hooks

useCoreQuery

Type-safe hook for core-scoped queries (operations that don’t require a library).
Features:
  • Auto-generated types - type and input are type-checked against Rust definitions
  • TanStack Query - Full TanStack Query API available
  • Automatic caching - Query results are cached by key
  • Type inference - data type is automatically inferred
Examples:

useLibraryQuery

Type-safe hook for library-scoped queries (operations within a specific library).
Features:
  • 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
Examples:

useNormalizedCache

Event-driven cache updates for real-time sync across devices. See Normalized Cache for full documentation.
When to use:
  • 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).
Features:
  • 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
Examples:

useLibraryMutation

Type-safe hook for library-scoped mutations (operations within a specific library).
Features:
  • 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
Examples:

Event Hooks

useEvent

Subscribe to specific Spacedrive events.
Features:
  • Event filtering - Only receives events of specified type
  • Automatic cleanup - Unsubscribes on unmount
  • Type-safe - Event type is checked at compile time
Examples:

useAllEvents

Subscribe to all Spacedrive events (useful for debugging).
Warning: This can be noisy. Use useEvent for specific events in production.

Custom Hooks

useLibraries

Convenience hook for fetching all libraries.
With stats:

Client Hook

useSpacedriveClient

Access the Spacedrive client instance directly.
Client methods:
  • client.setCurrentLibrary(id: string) - Switch to a library
  • client.getCurrentLibraryId() - Get current library ID
  • client.execute(method, input) - Execute RPC method directly
  • client.on(event, handler) - Subscribe to events
  • client.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

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
Use useCoreQuery and useLibraryQuery for data fetching, useCoreMutation and useLibraryMutation for mutations, and useEvent for event subscriptions.