Tips for fetching data with Sanity v3 in a React project

6 replies
Last updated: Jun 13, 2023
Good evening! I just took the step to work with v3 instead of v2, and got a studio up and running etc. I’m just now trying to find information how to go about fetching the data with v3. Can you use sanityClient or what do I use instead? I’ve been following Kapehe’s NEXT+Sanity-guide and some launch vids, but can’t find the right one. I’m working with React, no TS and no Next. Does anyone have tips on what articles to read or vids to watch to get a grip on the fetch-part I’d be very happy! Thanks in advance. ☺️
AI Update

Congrats on upgrading to v3! The good news is that in Sanity v3, you still use @sanity/client to fetch data, but you'll import createClient instead of the old sanityClient export.

Installation

First, install the client library:

npm install @sanity/client

Creating the Client

In v3, you use createClient from @sanity/client:

import { createClient } from '@sanity/client'

export const client = createClient({
  projectId: 'your-project-id', // find this in sanity.json or your Studio
  dataset: 'production', // or your dataset name
  useCdn: true, // set to false if you need fresh data
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
})

Fetching Data

Once you have your client configured, you can fetch data using GROQ queries:

import { client } from './client' // wherever you created your client

// In your React component
function MyComponent() {
  const [data, setData] = React.useState(null)

  React.useEffect(() => {
    client.fetch('*[_type == "post"]').then((posts) => {
      setData(posts)
    })
  }, [])

  if (!data) return <div>Loading...</div>
  
  return (
    <div>
      {data.map((post) => (
        <div key={post._id}>{post.title}</div>
      ))}
    </div>
  )
}

Key Differences from v2

  • Import createClient instead of the default export
  • The apiVersion parameter is now required (use a current date in YYYY-MM-DD format)
  • The client instance works the same way for .fetch(), .create(), .patch(), etc.

Additional Resources

The React single-page application guide might also be helpful, though it may reference some patterns you can adapt for vanilla React with React Router.

Hope this gets you up and running! 🚀

You can also just use the built in fetch web API and fetch data from sanity using their HTTP API: https://www.sanity.io/docs/http-api
Thanks! I think the first link was it. But I feel so stupid, i get this error when trying to import the function to one of my components.
Module not found: Error: Can't resolve '@sanity.config' in '/Users...
Am I missing something?
Clearly was. Working now. Thanks for the help
user R
🥳
Sweet! Don't feel too stupid. About 90% of the things I ask here I end up figuring out like 5 minutes later. 😁

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?