Sanity App SDK
    Preparing search index...
    • Retrieves the current Sanity instance or finds a matching instance from the hierarchy

      Parameters

      • Optionalconfig: SanityConfig

        Optional configuration to match against when finding an instance

      Returns SanityInstance

      The current or matching Sanity instance

      This hook accesses the nearest Sanity instance from the React context. When provided with a configuration object, it traverses up the instance hierarchy to find the closest instance that matches the specified configuration using shallow comparison of properties.

      The hook must be used within a component wrapped by a ResourceProvider or SanityApp.

      Use this hook when you need to:

      • Access the current SanityInstance from context
      • Find a specific instance with matching project/dataset configuration
      • Access a parent instance with specific configuration values
      // Get the current instance from context
      const instance = useSanityInstance()
      console.log(instance.config.projectId)
      // Find an instance matching the given project and dataset
      const instance = useSanityInstance({
      projectId: 'abc123',
      dataset: 'production'
      })

      // Use instance for API calls
      const fetchDocument = (docId) => {
      // Instance is guaranteed to have the matching config
      return client.fetch(`*[_id == $id][0]`, { id: docId })
      }
      // Find an instance with specific auth configuration
      const instance = useSanityInstance({
      auth: { requireLogin: true }
      })

      Error if no SanityInstance is found in context

      Error if no matching instance is found for the provided config