Optional
config: SanityConfigOptional configuration to match against when finding an instance
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:
// 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 })
}
Retrieves the current Sanity instance or finds a matching instance from the hierarchy