See Sanity in action 👀 Join us for a live product demo + Q&A →

Using the useClient hook in a Sanity V2 project

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

import { useClient } from 'sanity'

const client = useClient({ apiVersion: '2023-01-01' })
Feb 2, 2023, 9:59 PM
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'})
Feb 2, 2023, 10:04 PM
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?
import sanityClient from '@sanity/client';
Feb 2, 2023, 10:05 PM
Ah, the method I mentioned was for inside of the Studio. You’ll use the @sanity/client package on your frontend.
Feb 2, 2023, 10:06 PM
Cool, no worries, will give it a go 👍
Feb 2, 2023, 10:06 PM
do it
Feb 2, 2023, 10:06 PM
It’s worth the effort to migrate, I promise!
Feb 2, 2023, 10:07 PM
Yeah I want to, I did start doing it and ran into some kind of problem that I can't remember now
Feb 2, 2023, 10:07 PM
I'll see how patient the client is and maybe dive back in 😄
Feb 2, 2023, 10:09 PM
Oh I remember now, it was some kind of error being thrown related to ESM and styled-components
Feb 2, 2023, 10:10 PM
Ever heard of something like that?
Feb 2, 2023, 10:11 PM
Yeah, it sounds familiar. I’ll do some searching.
Feb 2, 2023, 10:30 PM
Oh that would be amazing if you can find something. I did quite a lot of googling but couldn't find a solution!
Feb 2, 2023, 10:30 PM – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.