1

I have multiple images instead of importing each image in my content.js like this eg:

import myimg1 from './myimg1.png'

import myimg2 from './myimg2.png'

import myimg3 from './myimg3.png'

import myimg4 from './myimg4.png'

I have made images.js and then imported each image inside images.js and exported it so that I can access those images in content.js:

images.js:

import java from './images/java.png';
import neural from './images/neural.png';
import logo from './images/logo.png';
import dsa from './images/dsa.png';
import dl from './images/dl.jpeg';
import ds from './images/ds.jpeg';
import boy from './images/boy.jpeg';
import ml from './images/ml.jpeg';
import phone from './images/phone.png';

export default {
    java,
    logo,
    dsa,
    dl,
    ds,
    boy,
    ml,
    neural,
    phone
}

In content.js:

import React, { Component } from 'react';
import images from './images';

<img src={images.java} alt="Java" height="65" width="65"></img>

 <img src={images.neural} alt="Neural Network" height="65" width="65"></img>

I have made images folder which contains all images but I am not able to access the images and display it in content.js component.

enter image description here

5
  • What will be the result of, for example, images.java? Commented Mar 13, 2018 at 4:02
  • @HuyVo I am using images.java in src="" check in the question Commented Mar 13, 2018 at 5:15
  • Have you tried to console.log images.java? The reason it doesn't render your image is because images.java is null. Commented Mar 13, 2018 at 6:14
  • @HuyVo I think you did not got my question I am importing all images from images folder inside images.js component and then exporting it to content.js but it is not working. Commented Mar 13, 2018 at 6:15
  • The reason it's not working is that you either imported or exported it the wrong way. Commented Mar 13, 2018 at 6:40

2 Answers 2

1

See there is no class named images in your images.js, so

import images from './images'

will do nothing in content.js...So try this way

images.js

import java from './images/java.png';
import logo from './images/logo.png';

export {
    java,
    logo
}

content.js

import React, { Component } from 'react';
import { java, logo } from './images';

<img src={java} alt="" height="65" width="65">
<img src={logo} alt="" height="65" width="65">
Sign up to request clarification or add additional context in comments.

2 Comments

Can you post the same answer here stackoverflow.com/questions/49229529/… I will upvote :)
@stonerock Glad it helped you..!..See there is no need to post the same answer on same question...I think you should delete the previous question...
0

the export default is just used for when there is only one import function, in your case you should do export without default

export {
    java,
    logo,
    dsa,
    dl,
    ds,
    boy,
    ml,
    neural,
    phone
}

Then, in your file, you'd import all inside brackets

import { java, logo, dsa.. } from './yourFilePath'

4 Comments

See the screenshots: imgur.com/a/NkedO it is saying export default.
I also made following changes in code see multiple screenshots: imgur.com/a/NlqNf
I think you did not got my question I am importing all images from images folder inside images.js component and then exporting it to content.js but it is not working.
if you look at this answer, is the same answer than @Bhuwan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.