Learning about props in react atm, I’m having an issue whereby when I try to pass an image as a prop its won’t display at all, see the screenshot below
import React from "react";
import PropTypes from "prop-types";
import { Link } from "react-router-dom";
const HeroSlider = (props) => {
const data = props.data;
return (
<div className="hero-slider">
{data.map((item, index) => (
<HeroSliderItem key={index} item={item} />
))}
</div>
);
};
HeroSlider.propTypes = {
data: PropTypes.array.isRequired,
};
const HeroSliderItem = (props) => (
<div className="hero-slider__item">
<div className="hero-slider__item__info">
<div className="hero-slider__item__info__title">
<span>{props.item.title}</span>
</div>
<div className="hero-slider__item__info__description">
<span>{props.item.description}</span>
</div>
<div className="hero-slider__item__info__btn">
<Link to={props.item.path}>
<button>Shop Now</button>
</Link>
</div>
</div>
<div className="hero-slider__item__image">
<img src={props.item.img} alt="" />
</div>
</div>
);
export default HeroSlider;
2
Answers
You are passing
item
intoHeroSliderItem
so you should fix your property path for Img Src:I recommend you rename the variable name of the parameter for your component to:
So you can access the
.img
property in a more semantic way:You can store images in variables using images relative path and then use it in your heroSliderData object like bellow.