Answer the question
In order to leave comments, you need to log in
How to catch props transfer error?
The page has cards with photos and names. The card component looks like this:
import Image from './Image';
function Card({ getCard, card }) {
function handleClickImage() {
getCard(card);
}
return (
<li className="element">
<button type="button" className="element__trash-button" />
<Image
onClick={handleClickImage}
src={card.link}
alt={card.name}
className="element__image"
/>
<div className="element__container">
<h2 className="element__title"> {card.name} </h2>
<div className="element__like-group">
<button type="button" className="element__like" />
<h3 className="element__like-counter"> {card.likes.length} </h3>
</div>
</div>
</li>
);
}
export default Card;
import Image from './Image';
function ImagePopup({ card, onClose }) {
return (
<section className={`popup ${card && 'popup_opened'}`}>
<div className="popup__zoom-image">
<button onClick={onClose} type="button" className="popup__button-close" />
<Image
src={card ? card.link : null}
alt={card ? card.name : null}
className="popup__image"
/>
<h2 className="popup__title-zoom-image"> {card ? card.name : null} </h2>
</div>
</section>
);
}
export default ImagePopup;
import { useState } from 'react';
import filler from '../images/filler.jpg';
function Image({ src, alt, onClick, className }) {
const [isLoadError, setIsLoadError] = useState(false);
function handleLoadError() {
setIsLoadError(true);
}
return (
<img
onClick={onClick}
src={isLoadError ? filler : src}
alt={`Фото: ${alt}`}
className={className}
onError={handleLoadError}
/>
);
}
export default Image;
Answer the question
In order to leave comments, you need to log in
what could be causing this?
useEffect(() => setIsLoadError(false), [ src ]);
<Image key={card ? card.link : 'hello, world!!'}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question