我是在 public 中存储 Image 资产还是在 reactJS 中存储 src?

我正在使用我的应用程序的反应。我有一个 div,我想有一个背景图像。但我没法让它显示出来。

当我把它作为 myapp/src/bgimage.png包含在 src文件夹中时,它工作得很好,但是我听说我应该把它包含在一个根目录级别的 images文件夹中,所以它是 myapp/images/bgimage.png,但是这对我来说不起作用,并且给我:

您试图导入. ./images/bgimage. png,它位于项目 src/目录之外

有人能告诉我在 reactJS 中包含图像资产的正确方法吗?

90112 次浏览

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.

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

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.

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.

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.

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

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.

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.