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

Reactjs

Reactjs Problem Overview


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?

Reactjs Solutions


Solution 1 - Reactjs

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.

Solution 2 - Reactjs

I would add that creating an "assets" folder inside the "src" folder is a good practice.

Solution 3 - Reactjs

Use /src if you are using create-react-app


If you are using create-react-app , You need to use /src for the following benefits.

> 1. Scripts and stylesheets get minified and bundled together to avoid extra network requests. 2. Missing files cause compilation errors instead of 404 errors for your users.

  1. Result filenames include content hashes so you don’t need to worry about browsers caching their old versions.

Also, if you are using webpack's asset bundling anyway, then your files in /src will be rebuilt.

> You may create subdirectories inside src. For faster rebuilds, only files inside src are processed by webpack. You need to put any JS and CSS files inside src, otherwise webpack won’t see them.

See this link

Solution 4 - Reactjs

No,

public folder is for static file like index.html and ...

I think you should make an "assets" folder in src folder and access them in this way.

Solution 5 - Reactjs

In this article, I mentioned that

> Keep an assets folder that contains top-level CSS, images, and font files.

In react best practices we keep an assets folder inside the src which may contain top-level CSS, images, and font files.

Solution 6 - Reactjs

In continuation with the other answers I would further like to add that you should create an 'assets' folder under 'src' folder and then create 'images' folder under 'assets' folder. You can store your images in the 'images' folder and then access them from there.

Solution 7 - Reactjs

According to the create-react-app documentation, regarding the use of 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.webmanifest. >* You have thousands of images and need to dynamically reference their paths. >* You want to include a small script like pace.js outside of the bundled code. >* Some libraries may be incompatible with webpack and you have no other option but to include it as a

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmohammad chughtaiView Question on Stackoverflow
Solution 1 - ReactjsMatt SaundersView Answer on Stackoverflow
Solution 2 - Reactjskrichey15View Answer on Stackoverflow
Solution 3 - ReactjsMJ StudioView Answer on Stackoverflow
Solution 4 - ReactjsAli AlizadehView Answer on Stackoverflow
Solution 5 - ReactjsSabesanView Answer on Stackoverflow
Solution 6 - ReactjsRupam KunduView Answer on Stackoverflow
Solution 7 - ReactjsAviv HadarView Answer on Stackoverflow