英文:
Using JavaScript's optional chaining to conditionally render an image from an API
问题
I'm working on building a very basic news app using the New York Times' 'top stories' API. The API is well documented and there are plenty of examples of how to get stared online. But I'm stuck with conditionally rendering an image. Some of the objects in the API do not contain image files. In those cases, I need to render a generic "no image available" .png file. I'm really new to React and APIs so still learning the basics.
My code is throwing this error: Uncaught TypeError: Cannot read properties of undefined (reading 'media-metadata')
.
I'm trying to conditionally render an image using JS Optional Chaining. This is what I have so far.
<div className="card">
{title}
<ImageFormat className={media} component="img" src={media[0]['media-metadata'][2]?.url ?
`${media[0]['media-metadata'][2].url}` : 'https://upload.wikimedia.org/wikipedia/commons/e/e0/Red_Exclamation_Dot.png'
}/>
{/* <img src={media[0]['media-metadata'][2].url} /> */}
<div className="facet">
{des_facet}
</div>
</div>
My logic is this: if the object contains the img file then it will render, if not, then render the png file.
As stated above, I've tried using JS optional chaining (which is a very new concept for me). The error is Uncaught Type Error: Cannot read properties of undefined (reading 'reading 'media-metadata')
英文:
I'm working on building a very basic news app using the New York Times' 'top stories' API. The API is well documented and there are plenty of examples of how to get stared online. But I'm stuck with conditionally rendering an image. Some of the objects in the API do not contain image files. In those cases, I need to render a generic "no image available" .png file. I'm really new to React and APIs so still learning the basics.
My code is throwing this error: Uncaught TypeError: Cannot read properties of undefined (reading 'media-metadata')
.
I'm trying to conditionally render an image using JS Optional Chaining. This is what I have so far.
<div className="card">
{title}
<ImageFormat className={media} component="img" src={media[0]['media-metadata'][2]?.url ?
`${media[0]['media-metadata'][2].url}` : 'https://upload.wikimedia.org/wikipedia/commons/e/e0/Red_Exclamation_Dot.png'
}/>
{/* <img src={media[0]['media-metadata'][2].url} /> */}
<div className="facet">
{des_facet}
</div>
</div>
My logic is this: if the object contains the img file then it will render, if not, then render the png file.
As stated above, I've tried using JS optional chaining (which is a very new concept for me). The error is Uncaught Type Error: Cannot read properties of undefined (reading 'reading 'media-metadata')
答案1
得分: 0
以下是您要翻译的代码部分:
while using the `&&` operator to check if each property is defined before accessing it. This way, if any of the properties are undefined, the code won't throw an error and will instead skip over the image rendering.
<div className="card">
{title}
{media && media[0] && media[0]['media-metadata'] && (
<ImageFormat
className={media}
component="img"
src={
media[0]['media-metadata'][2]?.url
? `${media[0]['media-metadata'][2].url}`
: 'https://upload.wikimedia.org/wikipedia/commons/e/e0/Red_Exclamation_Dot.png'
}
/>
)}
<div className="facet">{des_facet}</div>
</div>
英文:
while using the &&
operator to check if each property is defined before accessing it. This way, if any of the properties are undefined, the code won't throw an error and will instead skip over the image rendering.
<div className="card">
{title}
{media && media[0] && media[0]['media-metadata'] && (
<ImageFormat
className={media}
component="img"
src={
media[0]['media-metadata'][2]?.url
? `${media[0]['media-metadata'][2].url}`
: 'https://upload.wikimedia.org/wikipedia/commons/e/e0/Red_Exclamation_Dot.png'
}
/>
)}
<div className="facet">{des_facet}</div>
</div>
答案2
得分: 0
看起来 media[0]
是 undefined
,你需要使用 ?.
进行可选链操作。
<div className="card">
{title}
<ImageFormat className={media} component="img" src={media?.[0]?.['media-metadata']?.[2]?.url ? `${media[0]['media-metadata'][2].url}` : 'https://upload.wikimedia.org/wikipedia/commons/e/e0/Red_Exclamation_Dot.png'}/>
<div className="facet">
{des_facet}
</div>
</div>
英文:
It looks like media[0]
is undefined
you need to optionally chain it with ?.
.
<div className="card">
{title}
<ImageFormat className={media} component="img" src={media?.[0]?.['media-metadata']?.[2]?.url ? `${media[0]['media-metadata'][2].url}` : 'https://upload.wikimedia.org/wikipedia/commons/e/e0/Red_Exclamation_Dot.png'}/>
<div className="facet">
{des_facet}
</div>
</div>
答案3
得分: 0
如果media被初始化为一个数组,可以使用以下方式。不要使用三元运算符,因为它会导致我们重复相同的条件语句两次,而是使用**||**(或)运算符。如果之前的值为假(即未定义、null、0或""),它将继续并选择下一个。
src={media[0]?.['media-metadata']?.[2]?.url || "https://upload.wikimedia.org/wikipedia/commons/e/e0/Red_Exclamation_Dot.png"}
如果media可以是未定义或null,那么可以使用以下方式。
src={media?.[0]?.['media-metadata']?.[2]?.url || "https://upload.wikimedia.org/wikipedia/commons/e/e0/Red_Exclamation_Dot.png"}
英文:
If media is initialized as an Array.
Use this. Instead of using ternary here as it makes us to repeat same conditional statement twice,use || (OR) operator. If previous value is falsy (i.e undefined, null, 0, "") it will move forward and pick next.
src={media[0]?.['media-metadata']?.[2]?.url || "https://upload.wikimedia.org/wikipedia/commons/e/e0/Red_Exclamation_Dot.png"}
If media can be undefined or null then use this.
src={media?.[0]?.['media-metadata']?.[2]?.url || "https://upload.wikimedia.org/wikipedia/commons/e/e0/Red_Exclamation_Dot.png"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论