Is useClient hook documentation still accurate for Sanity Studio V2?

12 replies
Last updated: Feb 2, 2023
Hi all, I have a sanity project still running on studio V2. I want to use the
useClient
hook in my studio server code -- is this documentation still accurate?

import { useClient } from 'sanity'

const client = useClient({ apiVersion: '2023-01-01' })
AI Update

No, that documentation is not accurate for Studio v2. The useClient hook imported from 'sanity' is a Studio v3 feature and won't work in your v2 project.

For Sanity Studio v2, you need to use the parts system to import the client:

import sanityClient from 'part:@sanity/base/client'

// For a client with specific config
const client = sanityClient.withConfig({ apiVersion: '2023-01-01' })

The useClient hook as shown in that documentation was introduced in Studio v3 as part of the migration away from the parts system. In v3, the import pattern changed to:

// Studio v3 only
import { useClient } from 'sanity'

const client = useClient({ apiVersion: '2023-01-01' })

Important caveat: Since useClient is a React hook (in v3), it follows React's rules of hooks - meaning it can only be called inside React function components or custom hooks, not at the top level of a module or in regular server code.

If you're working in Studio v2 and need the client outside of a React component context, you can also create a configured client instance directly:

import sanityClient from '@sanity/client'

const client = sanityClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  apiVersion: '2023-01-01',
  useCdn: false,
  token: 'your-token' // if needed
})

Since you're still on Studio v2, stick with the part:@sanity/base/client import pattern with .withConfig() for your use case. If you're planning to upgrade to v3, be aware that Studio v4 (the current version) requires Node.js 20+ and has other breaking changes you'll need to account for.

Show original thread
12 replies
No, that’s a V3 hook that doesn’t exist in V2, unfortunately. You’ll want to use this method:
// In v2
import sanityClient from "part:@sanity/base/client"

const client = sanityClient.withConfig({apiVersion: '2021-03-25'})
user M
Thanks! Do I need to use the above exactly or could I also use the client package I'm using for the frontend?
e..g
import sanityClient from '@sanity/client';
Ah, the method I mentioned was for inside of the Studio. You’ll use the @sanity/client package on your frontend.
Cool, no worries, will give it a go 👍
do it
It’s worth the effort to migrate, I promise!
Yeah I want to, I did start doing it and ran into some kind of problem that I can't remember now
I'll see how patient the client is and maybe dive back in 😄
Oh I remember now, it was some kind of error being thrown related to ESM and styled-components
Ever heard of something like that?
Yeah, it sounds familiar. I’ll do some searching.
Oh that would be amazing if you can find something. I did quite a lot of googling but couldn't find a solution!

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?