reactjs - 如何通过道具将对象数组中的src传递给另一个组件,有人可以向我解释为什么我不能显示我的图像
问题描述
import React from 'react';
import Gallery from '../Gallery/Gallery';
const Galleries = (props) => {
const galleries = [
{
name: 'natur',
src: ' ../../Images/photographer-gfc1c015b1_1920.jpg',
},
{
name: 'moutain',
src: ' ../../Images/photographer-gfc1c015b1_1920.jpg',
},
];
return (
<div>
{/* generate the numbre of data */}
<h1>I have {galleries.length} Images</h1>
{galleries.map((gallery) => (
<Gallery name={gallery.name} galery={gallery.src} />
))}
</div>
);
};
export default Galleries;
import React from 'react';
const Gallery = (props) => {
return (
<div>
<h1>{props.name}</h1>
<img src={props.src} alt="" style={{ height: '200px', width: '300px' }} />
</div>
);
};
export default Gallery;
解决方案
这是你的错误,使用了错误的道具,你正在通过这个<Gallery name={gallery.name} galery={gallery.src} />
更改此行,这是错误行,使用src
,但您从未通过 src,
<img src={props.src} alt="" style={{ height: '200px', width: '300px' }} />
改成这个,这会很好,props.galery
工作
<img src={props.galery} alt="" style={{ height: '200px', width: '300px' }} />
推荐阅读
- yang - yang cli-show-template 不替换叶值
- pdf - 我可以将 Google Apps 脚本程序连接到外部 Tesseract OCR 软件以获得更强大的 OCR 吗?
- reactjs - 从其中一个道具中选择一种类型
- android - 如何更新可组合函数内的 RecyclerView 的数据?
- angular - NGRX 效果内的 concatLatestFrom 循环
- python - 打开文件时出现错误
- java - OutOfMemoryError:最坑的突变测试
- typescript - 如何从嵌套数组创建联合类型?
- laravel - Laravel - 从不同的表中获取总和并计算
- javascript - 如何使用 react-redux 概念在我的有效负载中显示我的引导模式?