Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I store Image assets in public or src in reactJS?

Tags:

reactjs

I am using react for my application. I have a div that I would like to have a background image. But I can't get it to show.

When I include it in the src folder as myapp/src/bgimage.png it works perfectly but I've heard that I should include it in a folder named images at the root level so it's myapp/images/bgimage.png, however this does not work for me and gives me:

You attempted to import ../images/bgimage.png which falls outside of the project src/ directory.'

Can anyone tell me the proper way to include image assets in reactJS?

like image 533
mohammad chughtai Avatar asked Jun 20 '17 02:06

mohammad chughtai


People also ask

Should images go in SRC or public 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.

Should I put images in public folder react?

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.


1 Answers

public: anything that is not used by your app when it compiles

src: anything that is used when the app is compiled

So for example if you use an image inside a component, it should be in the src folder but if you have an image outside the app (i.e. favicon) it should be in public.

like image 66
Matt Saunders Avatar answered Oct 11 '22 16:10

Matt Saunders