How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Sanity fetch returns undefined: "item is not defined" error in React

15 repliesLast updated: Nov 29, 2025

Hello! I'm building my first React app with Sanity following User's tutorial here . I've made a document type called 'item' in my Sanity Studio, but whenever I try to pull fields from it i.e. {item.title}, I get errors saying "'item' is not defined" at each line. I've set up my schemas as per the video, and I'm using this fetch:

useEffect(() => {
`sanityClient.fetch(
*[_type == "item"]{

title,

slug,

mainImage,

}
)`
.then((_data_) => setItemData(_data_))

.catch(console.error);

})
AI Update

I can see the issue in your code! You have a syntax error in your useEffect hook. The backticks () are in the wrong place - they should be wrapping the GROQ query string, not the entire sanityClient.fetch()` call.

Here's the corrected version:

useEffect(() => {
  sanityClient.fetch(
    `*[_type == "item"]{
      title,
      slug,
      mainImage,
    }`
  )
  .then((data) => setItemData(data))
  .catch(console.error);
}, []); // Don't forget the dependency array!

Key fixes:

Additional tips:

The "item is not defined" error you're seeing is likely because the fetch isn't working due to the syntax error, so itemData remains empty or undefined. Once you fix the backticks and add the dependency array, your data should load properly and you'll be able to access item.title when mapping over the results.

Show original thread
15 replies

Was this answer helpful?

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.

Related contributions