How to Fetch Image From Client - Blank Image in Response
{ name: 'image',
title: 'Image',
type: 'array',
of: [{ type: 'image' }], options: { hotspot: true,
}
},
I am giving {fetchedData.image} as src of imgimport React, { useEffect, useState, useLayoutEffect } from 'react';import { Link } from "react-router-dom";import Header from "../components/Header";import "./Home.css";
import {Carousel, Card, Image} from 'antd';import {client} from "../lib/client";const Home = () => { const [products,setProducts]= useState([]);const [bannerData,setBannerData]= useState([])useEffect(() => { `client.fetch(*[_type == "featured"]{ name,
genre,
image
}).then((data)=>setProducts(data))`
}, [])
useLayoutEffect(() => { `client.fetch(*[_type == "banner"]{ image
}).then((data)=>setBannerData(data))`
}, [])
return(
<>
<div className="container">
<Header/>
<Carousel autoplay className="carousel">{bannerData?.map((data) => { return <img src={urlFor(data.image).url()} className="carousel-img" alt="carousel"></img>;})}
</Carousel>
<div className="cards">
{products.map((product)=>{ return(
<Card className="card" key={product._id}><Link to="/categories" state={product.genre} className="link" style={{textDecoration:"none"}}><img src={urlFor(product.image).url()} alt={product.name} className="card-content"/><h4>{product.name}</h4> </Link>
</Card>
)
})}
</div>
</div>
</>
)
}
export default Home;
import sanityClient from '@sanity/client';import imageUrlBuilder from '@sanity/image-url'export const client = sanityClient({ projectId:'uykjgyz0',
dataset:'production',
apiVersion:'2022-03-10',
useCdn:true,
token:process.env.SANITY_TOKEN
});
const builder= imageUrlBuilder(client)
export function urlFor(source) { return builder.image(source)
}
urlForfunction wherever you want to use it, then pass your image asset to the function. It looks like you're doing that with your product image but not your banner data.
:43:1) at ImageUrlBuilder.url (builder.ts
:229:1) at Home.js
:42:1 at Array.map (<anonymous>)
at Home (Home.js
:37:1) at renderWithHooks (react-dom.development.js
:14985:1) at updateFunctionComponent (react-dom.development.js
:17356:1) at beginWork (react-dom.development.js
:19063:1) at HTMLUnknownElement.callCallback (react-dom.development.js
:3945:1) at Object.invokeGuardedCallbackDev (react-dom.development.js
:3994:1)
<img src={urlFor(person.image).width(200).invert().flipHorizontal().url()} />Now i tried this <img src={urlFor(`<https://cdn.sanity.io/images/uykjgyz0/production/${data.image.asset._ref.slice(6)}>`).url()}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.