How to read/display current dataset in Sanity Studio?
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 IDuseWorkspace()- Returns the entire workspace configuration, which includes the dataset, current user information, and other workspace settingsuseClient()- 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 thread3 replies
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.