When it comes to adding images in your JSX using Create React App, there are two different methods you can utilize. The first approach involves utilizing the import
statement to import an image located within the src
folder. Here's an example:
import person from '../images/image1.png'
<img src={person} alt="" />
The second method requires specifying the path of the image directly to the src
attribute of the <img>
tag. However, this path should always be relative to the public
folder and must start with /
, where /
represents the public
folder itself.
To learn more about adding images, fonts, and files, refer to the documentation on Adding Images, Fonts, and Files and Using the Public Folder available on Create React App's official website.
In order for the second method to function correctly, consider placing the images
folder inside the public
directory and accessing your images in the following manner:
<img src="/images/image1.png" alt="" />
If your React application is going to be deployed in a subfolder, you may need to specify the homepage in your package.json
file as
"homepage":"www.example.com/folder"
. Additionally, make use of process.env.PUBLIC_URL
as shown below to ensure the correct path for your images.
<img src={process.env.PUBLIC_URL + "/images/image1.png"} alt="" />