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>
)
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.
Where is images? what is this.props.level?
– Think-Twice
Aug 26 at 8:45