mohammad chughtai
mohammad chughtai

Reputation: 1964

Do I store Image assets in public or src in 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?

Upvotes: 156

Views: 155077

Answers (9)

Aakash
Aakash

Reputation: 23717

Difference between public and src/assets folders:

  • public folder is for anything that is being imported into your index.html
  • src/assets folder is for anything that is imported in your js/ts/jsx/tsx/...etc... files that are in the src folder.

Example of referring files that are in public folder

<!-- index.html -->
<head>
  <link rel="manifest" href="/manifest.json">
  <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
</head>

Example of referring files that are in the src/assets folder

// src/App.tsx

import Logo from "./assets/logo.svg"

export default () => {

   return <img src={Logo} /> 
}

Upvotes: 5

Kesavaraja Krishnan
Kesavaraja Krishnan

Reputation: 21

As per my understanding I will go with easier way. If you use your assets from public folder, after build contents from public will be maintained as same. So, if you deploy your app, the contents from public folder will also be loaded while your app loads. Assume your build is 5 MB (4 MB assets and 1 MB src) the 4 MB will get downloaded first then follows the src contains. Even if you use lazy and suspense your app will be slow during deployment.

Upvotes: 1

Ali Alizadeh
Ali Alizadeh

Reputation: 361

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.

Upvotes: 11

Aviv Hadar
Aviv Hadar

Reputation: 490

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 tag.

Upvotes: 13

MJ Studio
MJ Studio

Reputation: 4611

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.
  3. 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

Upvotes: 33

Sabesan
Sabesan

Reputation: 702

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.

Upvotes: 10

Rupam Kundu
Rupam Kundu

Reputation: 339

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.

Upvotes: 7

krichey15
krichey15

Reputation: 671

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

Upvotes: 60

Matt Saunders
Matt Saunders

Reputation: 4371

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.

Upvotes: 197

Related Questions