I am currently working on a feature that requires me to dynamically select the backgroundImage
based on the value of item.imageLinks.smallThumbnail
.
If the API response includes a URL for smallThumbnail
, I want to use that as the backgroundImage
. Otherwise, I would like to default to a local image called book.png
located inside ../public
.
However, I'm encountering issues with my code. When item.imageLinks.smallThumbnail
is not defined, it's not falling back to the alternate book.png
but instead throwing an error:
Type Error: Cannot Read 'smallThumbnail' of undefined
I would appreciate any help or guidance on this matter.
Thank you.
return(
<ol className="books-grid">
{book.map( (item) => (
<li key={item.id}>
<div className="book">
{item.shelf && (
<div className="book-top">
<div
className="book-cover"
style={{ width: 128, height: 193, backgroundImage: `url({${item.imageLinks.smallThumbnail} || ../public/book.png } )` }}></div>