These are my files, all the import routes are proper, This code used to work before I used react routing. I have no idea why it isn't working right now, would really appreciate it if anyone could help me solve this. I've been using react for less than a month now.
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App"
ReactDOM.render(<App />, document.getElementById("root"))
HomePage.jsx
import React from "react";
import Navbar from "../components/Navbar";
function HomePage() {
return (
<div>
<Navbar />
<h1>Homescreen</h1>
</div>
)
}
export default HomePage;
ProductScreen.jsx
import React from "react";
export default function ProductPage() {
return (
<h1>ProductPage</h1>
)
}
App.jsx
import React from "react";
import { Route, BrowserRouter as Router } from "react-router-dom";
// importing components
import Footer from "./Footer";
import Navbar from "./Navbar";
// importing pages
import ProductPage from "../pages/ProductScreen";
import HomePage from "../pages/HomePage";
function App() {
return (
<Router>
<Navbar />
<main>
<Route path="/" component={HomePage} > </Route>
<Route path="/product/:id" component={ProductPage} > </Route>
</main>
<Footer />
</Router>
)
}
export default App
Navbar.jsx
import React from "react";
function Navbar() {
return (
<header >
<h3>Amazona</h3>
<div >
<a href="cart.html">Cart</a>
<a href="signin.html">Sign in</a>
</div>
</header>
)
}
export default Navbar;
Footer.jsx
import React from "react";
function Footer() {
return (
<footer>
<p>All rights reserved</p>
</footer>
)
}
export default Footer;
My package.json
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.1",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
CodePudding user response:
If you don't know which version you use . please check package.json and search react-router-dom . Then you will see the version. (React router dom v6) If you use link ,You need to use router and routes. I think no error is shown your project. This is the documentation
https://reactrouter.com/docs/en/v6/getting-started/installation#create-react-app
This is demo code of routes , route
import * as React from "react";
import { Routes, Route, Link } from "react-router-dom";
function App() {
return (
<div className="App">
<h1>Welcome to React Router!</h1>
<Routes>
<Route path="/" element={<Home />} />
<Route path="about" element={<About />} />
</Routes>
</div>
);
}
If you Use V5
import {
BrowserRouter,
Switch,
Route,
Link,
useRouteMatch
} from "react-router-dom";
function App() {
return (
<BrowserRouter>
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/users">
<Users />
</Route>
</Switch>
</BrowserRouter>
);
}
react router dom v5 documentation https://reactrouter.com/docs/en/v6/upgrading/v5