这是我的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;
渲染这个的文件。
import React from 'react';
import ReactDOM from 'react-dom';
import Stopka from './Stopka';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<Stopka />, document.getElementById('stopka'));
registerServiceWorker();
现在我的图像没有在很多优化模式下渲染,因为如果我想要添加20甚至更多它将会非常痛苦。我想用循环或地图功能渲染它。尝试了一些,但它不起作用。你能解释一下我该怎么办?
这是我试过的。
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;
另一次尝试
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;
但是这得到错误“找不到模块:无法解析'./icons/{name}.png'”
答案 0 :(得分:6)
您可以将js map
功能与ES 6 template literals结合使用。
class Stopka extends Component {
render() {
let array = ["wood", "lake", "sun", "moon", "sea"];
let images = array.map(image => {
return <img key={image} src={require(`./icons/${image}.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;
答案 1 :(得分:1)
对于那些和我有同样问题的人。这也是一个有效的例子。 在我看来,关键索引更好,因为它不会在控制台中产生错误。 非常适合更大的项目。
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;
&#13;
感谢Paul Fitzgerald的指导。