I created a blog using React & Sanity but I couldn't find how to get the blogpost categories or category.
import {useState, useEffect} from "react"
import { Link, useParams } from "react-router-dom"
import client from "../client"
import BlockContent from "@sanity/block-content-to-react"
export default function SinglePost() {
const [singlePost, setSinglePost] = useState([])
const [isLoading, setIsLoading] = useState(true)
const { slug } = useParams()
useEffect(() => {
client
.fetch(
`*[slug.current == "${slug}"] {
title,
body,
categories {
category -> {
name
},
mainImage {
asset -> {
_id,
url
},
alt
}
}`
)
.then((data) => setSinglePost(data[0]))
setIsLoading(false)
}, [slug])
return (
<div>
<h2>{singlePost.categories.category.name}</h2>
</div>
Error : TypeError: Cannot read properties of undefined (reading 'category')