Javascript 如何使用地图或循环使用 React JS 渲染图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45713362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to render images with React JS using map or loop?
提问by Brunhilda
This is my js file, which contains my images.
这是我的 js 文件,其中包含我的图像。
import React, { Component } from 'react';
import './Stopka.css';
class Stopka extends Component {
render() {
return (
<div className="container">
<footer className="row">
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<h4>Some text</h4>
</div>
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<img src={require("./icons/name1.png")} alt="" className="img-responsive" />
<img src={require("./icons/name2.png")} alt="" className="img-responsive" />
<img src={require("./icons/name3.png")} alt="" className="img-responsive" />
<img src={require("./icons/name4.png")} alt="" className="img-responsive" />
</div>
</footer>
</div>
);
}
}
export default Stopka;
And file which render this.
和呈现这个的文件。
import React from 'react';
import ReactDOM from 'react-dom';
import Stopka from './Stopka';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<Stopka />, document.getElementById('stopka'));
registerServiceWorker();
For now my images is not rendering in much optimize mode, cuz if i want to add 20 or even more it'll be so much pain. I want to render it with loop or map function. Tried some but it doesn't work. Can you explain how can I do it?
现在我的图像没有在很多优化模式下渲染,因为如果我想添加 20 甚至更多,它会非常痛苦。我想用循环或地图功能渲染它。尝试了一些但它不起作用。你能解释一下我该怎么做吗?
This is what i tried.
这是我试过的。
import React, { Component } from 'react';
import './Stopka.css';
class Stopka extends Component {
render() {
let names = ['name1', 'name2', 'name3'];
for (let i = 0; i < this.props.level; i++) {
names.push(<image src={require("./icons/"+names+".png")} alt="" className="img-responsive" key={i} /> );
}
return (
<div className="container">
<footer className="row">
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<h4>Some text</h4>
</div>
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
{names}
</div>
</footer>
</div>
);
}
}
export default Stopka;
Another try
另一个尝试
import React, { Component } from 'react';
import './Stopka.css';
class Stopka extends Component {
render() {
let names = ['wood', 'sun'];
let images = names.map(name => {
<img
src = {require("./icons/{name}.png")}
alt = ""
className="img-responsive" />
});
return (
<div className="container">
<footer className="row">
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<h4>some text</h4>
</div>
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
{ images }
</div>
</footer>
</div>
);
}
}
export default Stopka;
But this get error "Module not found: Can't resolve './icons/{name}.png' "
但这会出现错误“找不到模块:无法解析'./icons/{name}.png'”
回答by Paul Fitzgerald
You can use the js mapfunction combined with ES 6 template literals.
class Stopka extends Component {
render() {
const array = ["wood", "lake", "sun", "moon", "sea"];
const images = array.map(image => {
return <img key={image} src={require(`./icons/${image}.png`)} className="img-responsive" />
});
return (
<div className="container">
<footer className="row">
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<h4>Some text</h4>
</div>
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
{ images }
</div>
</footer>
</div>
);
}
}
export default Stopka;
回答by Brunhilda
For others who had the same problem as me. This is working example as well. In my opinion with key index is better, cuz it not produce errors in console. Great for bigger projects.
对于和我有同样问题的其他人。这也是工作示例。在我看来,使用键索引更好,因为它不会在控制台中产生错误。非常适合更大的项目。
import React, { Component } from 'react';
import './Stopka.css';
class Stopka extends Component {
render() {
let names = ['wood', 'sun', 'moon', 'sea'].map( (name, index) => {
return <img key={index} className="img-responsive" alt="" src={require(`./icons/${name}.png`)} />
} );
return (
<div className="container">
<footer className="row">
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<h4>some text</h4>
</div>
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
{ names }
</div>
</footer>
</div>
);
}
}
export default Stopka;
Thanks to Paul Fitzgerald for the guide.
感谢保罗·菲茨杰拉德的指导。

