This is my js file, which contains my images.
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?
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' "
To use images in a React application, I start out by making a directory called Images in the src directory and then dragging and dropping image files from my computer into it. Then, when I want an image to appear in a particular place on the page, there are a few different options for getting it to show up.
In React, the map() function is most commonly used for rendering a list of data to the DOM. To use the map() function, attach it to an array you want to iterate over. The map() function expects a callback as the argument and executes it once for each element in the array.
React Native FastImage is a quick way to load images in React Native. All loaded pictures are aggressively cached by FastImage. You may add your own auth headers and preload pictures to your requests. GIF caching is also supported by react-native-fast-image.
You can use the js map
function 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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With