I am working on a website and trying to render a blockContent from Sanity.io, in my React application, but it's displayed as only [object Object] on the website. I tried different queries on Sanity Vision, and got the data but nothing seems to work, so any help would be much appreciated.
Here is my blockContent schema code:
export default {
title: 'Block Content',
name: 'blockContent',
type: 'array',
of: [
{
title: 'Block',
type: 'block',
// Styles let you set what your user can mark up blocks with. These
// correspond with HTML tags, but you can set any title or value
// you want and decide how you want to deal with it where you want to
// use your content.
styles: [
{title: 'Normal', value: 'normal'},
{title: 'H1', value: 'h1'},
{title: 'H2', value: 'h2'},
{title: 'H3', value: 'h3'},
{title: 'H4', value: 'h4'},
{title: 'Quote', value: 'blockquote'},
],
lists: [{title: 'Bullet', value: 'bullet'}],
// Marks let you mark up inline text in the block editor.
marks: {
// Decorators usually describe a single property – e.g. a typographic
// preference or highlighting by editors.
decorators: [
{title: 'Strong', value: 'strong'},
{title: 'Emphasis', value: 'em'},
],
// Annotations can be any object structure – e.g. a link or a footnote.
annotations: [
{
title: 'URL',
name: 'link',
type: 'object',
fields: [
{
title: 'URL',
name: 'href',
type: 'url',
},
],
},
],
},
},
// You can add additional types here. Note that you can't use
// primitive types such as 'string' and 'number' in the same array
// as a block type.
{
type: 'image',
options: {hotspot: true},
},
],
}
Here is the schema where I imported the blockContent:
export default {
name: "aboutme",
title: "About Me",
type: "document",
fields: [
{
name: "description",
title: "Description",
type: "blockContent",
},
{
name: "profileImg",
title: "Profile Picture",
type: "image",
options: {
hotspot: true,
},
},
{
name: "resume",
title: "Resume",
type: "file",
options: {
accept: "application/pdf",
},
},
],
};
Here is my jsx code where I am trying to display my contnent:
import React, { useState, useEffect } from "react";
import { motion } from "framer-motion";
import { images } from "../../constants";
import { client, urlFor } from "../../client";
import { AppWrap, MotionWrap } from "../../wrapper";
import "./About.scss";
import BlockContent from "@sanity/block-content-to-react";
import { useParams } from "react-router-dom";
const About = () => {
const [abouts, setAbouts] = useState([]);
const [aboutMe, setAboutMe] = useState({});
const [aboutMeData, setAboutMeData] = useState(null);
const { slug } = useParams();
useEffect(() => {
const aboutsQuery = '*[_type == "abouts"]';
const aboutMeQuery = `*[_type == "aboutme"][0]{
profileImg,
description[0]{children[0]{"short_description" : text}},
"resumeUrl": resume.asset -> url
}`;
client.fetch(aboutsQuery).then((data) => {
setAbouts(data);
});
client.fetch(aboutMeQuery).then((data) => {
setAboutMe(data);
console.log(data)
});
client.fetch(
`*[slug.current == "${slug}"]{
description[0]{children[0]{"short_description" : text}}
}`
)
.then((data) => setAboutMeData(data[0]));
}, []);
const viewResumeHandler = () => {
window.open(aboutMe.resumeUrl, "_blank");
};
return (
<>
<div className="app__about-context app__flex">
<div className="app__about-img app__flex">
<div className="app__flex">
<img
src={
aboutMe.profileImg
? urlFor(aboutMe.profileImg)
: images.aboutmine
}
alt="Profile"
/>
</div>
</div>
<div className="app__about-data app__flex">
<h2 className="head-text">About Me</h2>
<p
className="p-text"
id="p-text-id"
dangerouslySetInnerHTML={{ __html: aboutMe.description }}
></p>
<BlockContent
blocks={aboutMeData}
projectId="id" dataset="production"
/>
<div>
<button className="portfolio-button" onClick={viewResumeHandler}>
Resume
</button>
</div>
</div>
</div>
<div className="app__profiles">
{abouts.map((about, index) => (
<motion.div
whileInView={{ opacity: 1 }}
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 1.1 }}
transition={{ duration: 0.2, type: "tween" }}
className="app__profile-item"
key={about.title + index}
>
<img src={urlFor(about.imgUrl)} alt={about.title} />
<h2 className="bold-text" style={{ marginTop: "20px" }}>
{about.title}
</h2>
<h2 className="p-text" style={{ marginTop: "10px" }}>
{about.description}
</h2>
</motion.div>
))}
</div>
</>
);
};
export default AppWrap(
MotionWrap(About, "app__about"),
"about",
"app__whitebg"
);
The page displays but it only show [object Object] where I want to get the data displayed.
Thank you for taking the time!