我在使用JavaScript和React时遇到了一个错误。该应用程序是一个个人投资组合网站,我已经成功地从健全的数据为3个其他部分。现在,我正在处理Testimonials部分,我正在尝试拉取目前在Sanity Studio中加载的2个图像(jpeg):testimonials > imgurl在我的return语句中,我检查了testimonials的长度,这样我只在至少有一个存在的情况下才获取。在图像标记中,我使用src={urlFor(testimonials[currentIndex].imgurl)}
代码块:
return (
<>
{testimonials.length && (
<>
<div className="app__testimonial-item app__flex">
<img src={urlFor(testimonials[currentIndex].imgurl)} alt="testimonial" />
</div>
</>
)}
</>
);
1. urlFor is the function used to fetch images off Sanity, as per their documentation.
2. currentIndex is the useState inside of the Testimonial function, which acts as the container component for this section of the website. It will keep track of the state of the image being displayed and will cycle through others with a clickable action (code not present here)
**Question:** I am getting an error that is caused by the img tag and cannot find anything online about a fix. This [thread][1] does not fix my issue.
**ERROR MESSAGE**:
react-dom.development.js:20085 The above error occurred in the <img> component:
at img
at div
at Testimonial (http://localhost:3000/main.20b2c7aaa42080546b80.hot-update.js:37:90)
at div
at VisualElementHandler (http://localhost:3000/static/js/bundle.js:119980:38)
at MotionComponent (http://localhost:3000/static/js/bundle.js:119880:20)
at HOC
at div
at div
at HOC
at div
at App
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.
Error Message
Uncaught Error: Unable to resolve image URL from source (undefined)
at urlForImage (urlForImage.ts:43:1)
at ImageUrlBuilder.url (builder.ts:229:1)
at ImageUrlBuilder.toString (builder.ts:234:1)
at setValueForProperty (react-dom.development.js:672:1)
at setInitialDOMProperties (react-dom.development.js:8931:1)
at setInitialProperties (react-dom.development.js:9135:1)
at finalizeInitialChildren (react-dom.development.js:10201:1)
at completeWork (react-dom.development.js:19470:1)
at completeUnitOfWork (react-dom.development.js:22815:1)
at performUnitOfWork (react-dom.development.js:22787:1)
[1]: https://stackoverflow.com/questions/69031649/error-unable-to-resolve-image-url-from-source-null
2条答案
按热度按时间j2cgzkjk1#
转到模式中的您的证明文件并更改此部分:
cvxl0en22#
就用这个吧-对你有用
而不是使用imgurl或使用url()。我也不明白你为什么要使用[currentIndex]。但无论如何..