Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS and images in public folder

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

like image 629
Aceconhielo Avatar asked Oct 17 '22 21:10

Aceconhielo


People also ask

Should images be in public folder React?

When to Use the public Folder​ Normally we recommend importing stylesheets, images, and fonts from JavaScript. The public folder is useful as a workaround for a number of less common cases: You need a file with a specific name in the build output, such as manifest.

Should images be in public or SRC React?

You should avoid using images from the public/ directory in your React components because they are used without being imported. For example, if you have an image located at public/images/thumbnail. webp , you would set the src of the img to "/images/thumbnail.

How do I view public images in React?

To add images in public folder with React, we get the path to the public folder with process. env. PUBLIC_URL and concatenate the relative image path to it. <img src={process.


1 Answers

You don't need any webpack configuration for this.

In your component just give image path. React will know its in public directory.

<img src="/image.jpg" alt="image" />
like image 340
Bimal Grg Avatar answered Oct 20 '22 22:10

Bimal Grg