55

The problem I faced is with the img tag. When a single image is concerned, The below code loads the image:

import image1 from './images/image1.jpg';
<img src={image1} />

But the below code doesn't load:

 <img src={'./images/image1.jpg'}/>
            or
  <img src='./images/image1.jpg'/>

I need to loop through json, something like:

[{'img':'./images/image1.jpg','name':'AAA'}, {'img':'./images/image2.jpg','name':'BBB'}]

Plus, display each of them as image with name as footer. Looping is fine but the images doesn't load. It is not actually possible for me to import every images to add. I don't use anything other than JSX as of now. Please favour.

Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53
Gayathri
  • 1,776
  • 5
  • 23
  • 50

6 Answers6

78

You need to require the file like so:

<img src={ require('./images/image1.jpg') } />
Win
  • 5,498
  • 2
  • 15
  • 20
  • 2
    eslint is complaining about this says "Unexpected require(). (global-require)" Also says "Calls to require() should use string literals (import/no-dynamic-require)" – pixelwiz Dec 08 '17 at 01:11
  • @pixelwiz If you have enabled that setting replace the ' with backticks `. – Win Dec 09 '17 at 17:38
  • I actually had to make a separate file that imports all the images, then I could import that file and reference the images. Feels kinda crazy but now React, Webpack and ESLint seem happy. – pixelwiz Dec 11 '17 at 01:03
25

We could use require instead of import statment. Like ,

<img src={require("folder/image.format")} alt="image not found" />

But if you run it , it will show image not found!!!

We could simply solve it by changing the statment by adding .default

let image = require("folder/image.format");

<img src={image.default} alt="image not found" />
VELDAS R DURAI
  • 301
  • 4
  • 9
22

require is used for static "imports", so you just need to change your imports.

Example:

var imageName = require('./images/image1.jpg')
<img src={imageName} />
Robsonsjre
  • 1,586
  • 11
  • 17
1

I just tried this, it works!

import I01d from '../../styles/images/01d.png';
....
<img src={this.getWeatherIconImage(iconCode)}/>

getWeatherIconImage(icon) {
  switch (icon) {
    case '01d': return I01d; ...
    default: break;
  }
}
Daniel C. Deng
  • 1,573
  • 13
  • 8
1

The suggested answers do not appear to work any more. Here is a code fragment that highlight the issue.

import React from 'react';
import importImg from '../images/img.jpg';
export default function ImgTest(){
return(<>
<img src={importImg} alt='import'></img><br/>
<img src={require('../images/img.jpg')} alt='require function fails'></img><br/>
</>)
}

When this code is rendered, the following occurs.

1st displays the image - it the expected result

2nd displays the alt "require function fails" - is not the expected result

Bruce
  • 119
  • 2
  • 10
0

Its because, you've put your images folder in src folder. So you should import or require it. You can directly put the source of image if it's placed in public folder without importing or using require, like this.

<img src='/images/image1.jpg' />

Alfie
  • 162
  • 1
  • 6