react import image folder code example

Example 1: import img react

import shoe1 from './img/shoe_01.jpg'
const Shoe = (e) => {
	return ( 
        <div className="shoe-container">
            <img src={shoe1} alt=""/>
        </div>
    );
}

Example 2: import all images from folder reactjs

function importAll(r) {
  let images = {};
  r.keys().map((item, index) => { images[item.replace('./', '')] = r(item); });
  return images;
}

const images = importAll(require.context('./images', false, /\.(png|jpe?g|svg)$/));

<img src={images['doggy.png']} />

Example 3: import img react in another file

//import imgs in a different js file
import lostImage from './assets/img/illustrations/lost.svg';
export { lostImage }; 

//import in the component to use it
import {lostImage} from './images.js'

Example 4: React import image with url

import React from 'react';
import ReactDOM from 'react-dom';

const App: FunctionComponent = () => {
  return (
    <img src = {this.props.url} alt='something'/> 
  );
}

Tags:

Css Example