I am learning react. I am trying to display an image and background image from an array but it's not working.
I am getting the below output.
For image
<img src="images/system_analysis.png" alt="">
For Background image
<div class="proWrapper" style="background-image: url("images/system_analysis.png");"</div>
I need to know where I have to use require
for my images. I tried to use it in the array but it's not working.
imgIcon:require+"images/system_analysis.png",
I tried below code as well but it's displaying an error
<img src={require(props.imgIcon)} alt="" />
Note: I have more than 100 images and each image I can't import them manually
I am sharing my code here.
Main.js
import React from 'react';
const OurService = (props) => {
return (
<img src={props.imgIcon} alt="" />
);
}
const OurProducts = (props) => {
return (
<div className="proWrapper" style={{backgroundImage: "url("+ props.bg_image + ")"}}></div>
);
}
export {OurService,OurProducts}
App.js
import React from 'react';
import {OurService,OurProducts} from './Main'
import 'bootstrap/dist/css/bootstrap.min.css';
import './App.css';
const AService = () => {
const serviceArray=[
{
imgIcon:"/images/system_analysis.png"
},
{
imgIcon:"/images/system_analysis.png"
},
{
imgIcon:"/images/system_analysis.png"
},
{
imgIcon:"/images/system_analysis.png"
}
]
const serviceFor=serviceArray.map((storeServiceArray, i) => {
return <OurService key={i} imgIcon={serviceArray[i].imgIcon} />
})
return (
<div className="container">
<div className="equalPadding">
<div className="section-title text-center"><h2>Our Services</h2><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p></div>
<div className="row">
{serviceFor}
</div>
</div>
</div>
);
}
const AProducts = () => {
const productsArray=[
{
bg_image:"images/system_analysis.png"
},
{
bg_image:"images/system_analysis.png"
},
{
bg_image:"images/system_analysis.png"
}
]
const productsFor=productsArray.map((storeProductsArray, i) => {
return <OurProducts key={i} bg_image={productsArray[i].bg_image} />
})
return (
<div className="container">
<div className="equalPadding">
<div className="section-title text-center"><h2>Our Products</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p></div>
<div className="row">
{productsFor}
</div>
</div>
</div>
);
}
export {AService, AProducts}
Index.js
import React from 'react';
import ReactDOM from 'react-dom';
import {AService, AProducts} from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<AService />,document.getElementById('a-services'));
ReactDOM.render(<AProducts />,document.getElementById('a-products'));
serviceWorker.unregister();