Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images not loading from Public folder (using create react app)

wondering if anyone can help me. Im following a tutorial which has told me to put the images in the public folder in an app created with create-react-app

'./img/newyork.jpeg'

this is the path the tutorial told me to use for images in the public folder however the images aren't loading and i cant understand why any help would be much appreciated

Build File Structure

like image 497
Wekiban Avatar asked May 21 '26 07:05

Wekiban


1 Answers

You shouldn't keep any image assets in the public folder other than favicons etc See this thread also: The create-react-app imports restriction outside of src directory (TLDR: "This is special restriction added by developers of create-react-app. It is implemented in ModuleScopePlugin to ensure files reside in src/. That plugin ensures that relative imports from app's source directory don't reach outside of it.")

Generally in a create-react-app I would import images like so:

import lovelyImage from 'images/lovely-image.jpg'

and then rendered:

<img src={lovelyImage} alt={''} />

(the example above would assume the images directory is in the src directory (not public))

like image 59
HAS-Jack Avatar answered May 23 '26 00:05

HAS-Jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!