I need to display a fallback image when the original url request returns with a 404. The image wont return an error until it's called upon and I can't figure out how to access that 404 to redirect it.
the fallback image has been imported from my local files
My if statement in checkImg = (props) => {}
is completely disregarded as it is now.
export default class FirstListPage extends React.Component{
checkImg = (product) => {
if(product.imgurl === null){
return <img src={fallback} alt='default' />
}
return <img src={product.imgurl} alt='first choice' />
}
render(){
if (this.props.firstList.data === undefined){
return null
}
return(
<div className='card-wrapper'>
{this.props.firstList.data.map(product =>
<div className='card'
key={product.id}>
{this.checkImg(product)}
<h3>{product.title}</h3>
<p>{product.description}</p>
</div>
)}
</div>
)
}
}
The only other thing I can think of would be to modify my .catch
but I cant wrap my head around how I would specify such a case.
App.js
componentDidMount() {
Promise.all([
fetch(`${config.API_ENDPOINT}/first`),
fetch(`${config.API_ENDPOINT}/second`),
fetch(`${config.API_ENDPOINT}/third`)
])
.then( ([firstRes, secondRes, thirdRes]) => {
if (!firstRes.ok)
return firstRes.json()
.then( e => Promise.reject(e))
if (!secondRes.ok)
return secondRes.json()
.then(e => Promise.reject(e))
if (!thirdRes.ok)
return thirdRes.json()
.then(e => Promise.reject(e))
return Promise.all([ firstRes.json(), secondRes.json(), thirdRes.json() ])
})
.then(([first, second, third]) => { this.setState({firstList, secondList, thirdList}) })
.catch(error => { console.error({error}) })
}
Data example
firstList: {
itemsPerPage: 6,
pages: 12,
data: [
{
id: 1,
imgurl:'https://website.com/image.jpg',
title: 'shoes',
description: 'desc here'
},
]
}