How to read/display current dataset in Sanity Studio?

3 replies
Last updated: Sep 27, 2023
Hi all. I would like to read and display the current dataset being loaded on the desk. Is there a way to read the current dataset?
AI Update

Yes! There are several hooks available to read the current dataset being loaded in the Sanity Studio desk. The most direct way is to use the useDataset() hook.

Here's a simple example of how to use it:

import { useDataset } from 'sanity'

export function MyComponent() {
  const dataset = useDataset()
  
  return (
    <div>Current dataset: {dataset}</div>
  )
}

The useDataset() hook returns the name of the current dataset as a string. This is particularly useful when you need to:

  • Display different content based on the dataset (e.g., production vs. staging)
  • Show the current dataset to users in your custom Studio components
  • Conditionally render components based on which dataset is active

If you need more information beyond just the dataset name, you might also find these related hooks helpful:

  • useProjectId() - Returns the current project ID
  • useWorkspace() - Returns the entire workspace configuration, which includes the dataset, current user information, and other workspace settings
  • useClient() - Returns a configured Sanity client instance for the current project and dataset

All of these hooks are available from the 'sanity' package and can be used in any custom React component within your Studio.

You can find more details about these hooks in the Studio React Hooks documentation.

Show original thread
3 replies
πŸ‘‹ What context are you trying to display it in? Most config options receive the dataset in their arguments while the
useDataset
hook can be used in React components.
Just used the
useDataset
hook but it only lists all the datasets.
Trying to have it show what dataset was deployed.
Upgrading to v3 handled this. Thanks

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?