gpt4 book ai didi

javascript - 如何使用映射或循环使用 React JS 渲染图像?

转载 作者:可可西里 更新时间:2023-11-01 02:40:50 26 4
gpt4 key购买 nike

这是我的 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 个或更多,那将是非常痛苦的。我想用循环或 map 功能渲染它。尝试了一些但它不起作用。你能解释一下我该怎么做吗?

这是我试过的。

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'”

最佳答案

您可以使用 js map功能结合 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;

关于javascript - 如何使用映射或循环使用 React JS 渲染图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45713362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com