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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With