172

Im new in ReactJS and I want to import images in a component. These images are inside of the public folder and I do not know how to access the folder from the react component.

Any ideas ?

EDIT

I want to import an image inside Bottom.js or Header.js

The structure folder is:

enter image description here

I do not use webpack. Should I ?

Edit 2

I want to use webpack for loading the images and the rest of assets. So in my config folder I have the next files:

enter image description here

Where I need to add the paths of the images and how?

Thanks

Aceconhielo
  • 2,636
  • 3
  • 14
  • 25

13 Answers13

301

You don't need any webpack configuration for this..

In your component just give image path. By default react will know its in public directory.

<img src="/image.jpg" alt="image" />
Mussa
  • 1,253
  • 19
  • 23
Bimal Grg
  • 6,230
  • 2
  • 23
  • 20
155

To reference images in public there are two ways I know how to do it straight forward. One is like above from Homam Bahrani.

using

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

And since this works you really don't need anything else but, this also works...

    <img src={window.location.origin + '/yourPathHere.jpg'} />
Gerard
  • 1,800
  • 1
  • 8
  • 6
  • 5
    It's not showing any error in the console but the image is appearing as a default image icon not what I have imported. I have tried both the cases you have mentioned. Please help me out. Thanks ! – Prakhar Mittal Mar 17 '19 at 22:58
20

the react docs explain this nicely in the documentation, you have to use process.env.PUBLIC_URL with images placed in the public folder. See here for more info

return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;
Hom Bahrani
  • 2,299
  • 24
  • 22
6

1- It's good if you use webpack for configurations but you can simply use image path and react will find out that that it's in public directory.

<img src="/image.jpg">

2- If you want to use webpack which is a standard practice in React. You can use these rules in your webpack.config.dev.js file.

module: {
  rules: [
    {
      test: /\.(jpe?g|gif|png|svg)$/i,
      use: [
        {
          loader: 'url-loader',
          options: {
            limit: 10000
          }
        }
      ]
    }
  ],
},

then you can import image file in react components and use it.

import image from '../../public/images/logofooter.png'

<img src={image}/>
Hassan Ajaz
  • 553
  • 6
  • 12
5

Create a folder in public ex.Assets and put your image in that folder and assign the folder_name / image_name in src

<img src = "/Assets/cardimg.svg" alt="Card image cap" width="400" />
4

We know React is SPA. Everything is rendered from the root component by expanding to appropriate HTML from JSX.

So it does not matter where you want to use the images. Best practice is to use an absolute path (with reference to public). Do not worry about relative paths.

In your case, this should work everywhere:

"./images/logofooter.png"

Lauren Rutledge
  • 1,145
  • 5
  • 20
  • 27
Karthik Sagar
  • 424
  • 4
  • 7
2

You should use webpack here to make your life easier. Add below rule in your config:

const srcPath = path.join(__dirname, '..', 'publicfolder')

const rules = []

const includePaths = [
  srcPath
]
    // handle images
    rules.push({
      test: /\.(png|gif|jpe?g|svg|ico)$/,
      include: includePaths,
      use: [{
        loader: 'file-loader',
        options: {
          name: 'images/[name]-[hash].[ext]'
        }
      }

After this, you can simply import the images into your react components:

import myImage from 'publicfolder/images/Image1.png'

Use myImage like below:

<div><img src={myImage}/></div>

or if the image is imported into local state of component

<div><img src={this.state.myImage}/></div> 
Umesh
  • 2,594
  • 16
  • 21
  • I doubt if you need webpack to be able to do an import of a resource. – Siya Mzam Nov 09 '17 at 08:25
  • @fshock, I said it will make it easier to accomplish the task. – Umesh Nov 09 '17 at 08:28
  • @Umesh I prefer to do it with Webpack. So you said that I have to add the rule in the config but which file ? I have path.js, polyfills.js, webpack.config.dev.js , webpack.config.prod.js and webpackDevServer.config.js. I'm going to edit the question so all of you can see the structure. Thanks – Aceconhielo Nov 09 '17 at 08:33
  • @Aceconhielo, you should include it in webpack.config.dev.js and webpack.config.prod.js – Umesh Nov 10 '17 at 01:17
2

here is sure and three simple way to do that...

  1. you can make one folder in directory and access it as we do import way or

  2. you can give direct image name in src

    <img src="image__name" alt="yourpic" /> 
    

//by default react look in public folder can render image in img tag

  1. const image = window.location.origin + "/image.png"; 
    

// if your image in public directory inside folder imagecollection than you can import it in this way

  const image = window.location.origin + "/imagecollection /image.png";
 <img src={image} alt="yourpic" />
vibhu
  • 183
  • 10
2

Simply Use

<img src='/image.extension' />

React will automatically point toward the public directory

Sandeep Rana
  • 101
  • 1
  • 5
0

Try This One its easy and Simple

  1. Don't Make Image folder in src.
  2. Make an image folder in public.
  3. you work in react-bootstrap install

npm install react-bootstrap

import React from 'react';
  
import 'bootstrap/dist/css/bootstrap.min.css';
import { Image } from 'react-bootstrap';
export default function Main() {
   
   return (
     <>
          <Image src="/img/image.jpg/100px250" alt="bg image"  fluid />
     </>
    )
 }

Hope it's Done

Sarthak Raval
  • 618
  • 1
  • 5
  • 21
0

A simple solution is to use paths like this /images/logoFooter.png. If the file is located directly under the public folder, do /someImage.png. You can go deeper, /x/y/z/image.png. Treat the locating part of the image as an absolute kind of location for that image.

For more information, check out https://create-react-app.dev/docs/using-the-public-folder/.

Saif Ul Islam
  • 309
  • 3
  • 13
0

if you want to add your javascript file from public folder to react, put specific file to index.html file in public folder. Your problem will be solve.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 20 '22 at 07:20
-2

You Could also use this.. it works assuming 'yourimage.jpg' is in your public folder.

<img src={'./yourimage.jpg'}/>
Suraj Rao
  • 28,850
  • 10
  • 94
  • 99