I just started learning React and I’m trying to build a static HTML page. Everything works fine except the React logo image which has refused to show.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<div id="root"></div>
<script src="index.js"></script>
</body>
</html>
import React from "react"
import ReactDOM from "react-dom/client"
const page = (
<div>
<img src="first-project/public/logo512.png" width="40px"/>
<h1>Fun facts about React</h1>
<ul>
<li>Was released in 2013</li>
<li>Was originally created by Jordan Walke</li>
<li>Has well over 100k stars on Github</li>
<li>Is maintained by facebook</li>
<li>Powers thousands of enterprise apps, including mobile apps</li>
</ul>
</div>
)
const root = ReactDOM.createRoot(document.getElementById("root"))
root.render(page)
I copied the image from my public
folder and for some reason it does not display properly on the web browser.
The image below shows how the image appears on the webpage
The image below shows my IDE:
3
Answers
Udate your image url as
if you want to use your method, add images in
src
folder only, which is not recommended.Try use
/public/logo512.png
as image url.This should be correct path.
Simply use
React by default points assets from public folder.