Image loop is not working in react js?

Image loop is not working in react js?



Images loop is not working. loop is working, but shows the string data a,b,c not the image source file. How can i show the images?


import React, Component from 'react';
import './Image.css';

export default class Imgs extends Component
render()

const names = ['a', 'b', 'c'];
for (let i = 0; i < this.props.level; i++)
names.push(<img src=require("./icons/"+names+".jpg") alt="" className="img-responsive" key=i /> );

return (
<div>
<div className="col-xs-12 col-sm-6 col-md-6 col-lg-6">
names
</div>
</div>
)






Where is images? what is this.props.level?
– Think-Twice
Aug 26 at 8:45





Do you have installed module in webpack for bundling images with jpg extensions? I think for images like this you need to put them inside variable using require first and than you can use them in React without the help of webpack.
– Lazar Nikolic
Aug 26 at 8:46






no I have not installed
– MD.ALIMUL Alrazy
Aug 26 at 8:48





(sorry I have misinterpreted your question in my answer)
– Dan
Aug 26 at 8:51





@Dan, I don't think you have. It seems to me that your answer was good and that it would work.
– Lazar Nikolic
Aug 26 at 8:52




1 Answer
1



You need to map the original array, rather than push to it:


const names = ['a', 'b', 'c'];
const imgs = names.map(function(name, i)
return <img src=require("./icons/"+ name +".jpg") alt="" className="img-responsive" key=i />
);

return (<div>imgs</div>);



It's also unclear why you looped from 0 to this.props.level, but I hope this answer offers you some guidance.



Also, as Lazar mentioned in a comment, you'll need to have a way for Webpack to handle your .jpg files, such as the file-loader loader.


file-loader





Error: Cannot find module './a,b,c.jpg'. showing this error
– MD.ALIMUL Alrazy
Aug 26 at 8:58





See require("./icons/"+ name +".jpg") instead of require("./icons/"+ names +".jpg") (I copied a typo from your question).
– Dan
Aug 26 at 9:01


require("./icons/"+ name +".jpg")


require("./icons/"+ names +".jpg")






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)