[英]Rendering multiple images using Props in React.js
I am trying to render images in my local system to a website using react components.我正在尝试使用反应组件将本地系统中的图像渲染到网站。 In the "index.js" file I am creating 4 different components with attributes.在“index.js”文件中,我创建了 4 个具有属性的不同组件。 One of the attributes is the "img".属性之一是“img”。 In the "Card.js" file I have created a card component and want to render this 4 times on the page.在“Card.js”文件中,我创建了一个卡片组件,并希望在页面上呈现 4 次。 But, I am unable to link the image to the component properly hence it's not loading.但是,我无法将图像正确链接到组件,因此它没有加载。 I have attached the screenshots and the relevant code below.我附上了截图和下面的相关代码。
Component that I need to render multiple times using props我需要使用道具多次渲染的组件
Index.js file where I am passing props to components我将道具传递给组件的 Index.js 文件
You should import the image, using您应该导入图像,使用
<Card img={require('./mj.jpg')} />
or或者
import mj from './mj.jpg';
<Card img={mj} />
Some reference: How do I reference a local image in React?一些参考: 如何在 React 中引用本地图像?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.