Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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']} />
Comment

import an image react in the public folder

<img src={process.env.PUBLIC_URL + '/yourPathHere.jpg'} />
<img src={window.location.origin + '/yourPathHere.jpg'} />  
Comment

import an image react in the public folder

<img src={process.env.PUBLIC_URL + '/yourPathHere.jpg'} />
<img src={window.location.origin + '/yourPathHere.jpg'} />  
Comment

Import all images from folder react

// Original answer by Kind Kouprey, additional comments and integer index alternative by me.

function importAll(r) {
  
  //// Use these line if you like to access arrays using an integer index.
  //let images = [];
  //r.keys().map((item, index) => { images.push(r(item)); });
  
  ////Use these line if you want to access each image using the file name.
  //let images = {};
  //r.keys().map((item, index) => { images[item.replace('./','')] = r(item); });
  
  return images;
}

//Param 1: The path or route to the directory you want to import.
//Param 2 (optional): Include directories?
//Param 3 (optional): Use a regular expression (regex) such as "/.(png|jpe?g|svg)$/" to select only certain file types.
const images = importAll(require.context('./images', false, /.(png|jpe?g|svg)$/));

//The index can be up to the number of images in the folder - 1;
<img src={images[0]} />
<img src={images[3]} />
//Or if you used the file name method...
<img src={images["doggy.png"]} />
Comment

react js public folder image path search

img elements must have an alt prop, either with meaningful text, or an empty string for decorative images
Comment

PREVIOUS NEXT
Code Example
Javascript :: public url react for serving django static in production 
Javascript :: explicitly import from lodash 
Javascript :: date et heure javascript 
Javascript :: GLTF position three.js 
Javascript :: input should reject non-alphabetical input reacj js 
Javascript :: reindex api ealtic search 
Javascript :: complite nodejs remove ubuntu 
Javascript :: how to difference of arrey object 
Javascript :: what is renderer in three.js 
Javascript :: react spring bounce in animation 
Javascript :: what is react-instantsearch-dom 
Javascript :: Angular active router change event 
Javascript :: I want to filter the below json data by start date and end date, it should return the data between start date and end date, 
Javascript :: how to uitree clone in jquery 
Javascript :: Will Yield function Model 
Javascript :: js template literal avoid white spaces 
Javascript :: react axios POST with super constructor parent class 
Javascript :: How to Solve the Staircase Problem with 5 Lines of JavaScript 
Javascript :: netsuite get search column value suitescript 
Javascript :: TOP Array Methods 
Javascript :: $() in javascript 
Javascript :: javascript pad 
Javascript :: aysnc and await response data usage 
Javascript :: Search an elemnt in a sorted and rotated array 
Javascript :: increment number in for loop javascript 
Javascript :: convert .js file to ts 
Javascript :: auto linting and testing in react tyescript 
Javascript :: on page navigate event javascript 
Javascript :: traversing 2d array javascript 
Javascript :: uncheck all other checkboxes when one is checked 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =