I'm new to ReactJS and I'm trying to create an app that has a homepage and an about page. I want to be able to switch between the two pages by clicking on the respective anchor/link in the navigation bar.
Everything was rendering fine before adding the BrowserRouter and the related elements to my code, but now nothing renders on the web page.
The version of react-router-dom I'm using is [email protected].
Here is my index.js file:
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(<App />, document.getElementById("root"));
App.js that makes use of a Bootstrap example:
import React from "react";
import { Link, BrowserRouter, Route, Routes } from "react-router-dom";
import About from "./About";
export default function App() {
return (
<div>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<Link class="navbar-brand" to="/"><i class="bi bi-yin-yang"></i></Link>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<Link class="nav-link active" aria-current="page" to="/">Home</Link>
</li>
<li class="nav-item">
<Link class="nav-link" to="/about">About Us</Link>
</li>
</ul>
</div>
</div>
</nav>
<BrowserRouter>
<Routes>
<Route path="/" element={<App />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
</div>
);
};
And here is the About.js file:
import { Fragment } from "react";
export default function About() {
return (
<Fragment>
<h1>About Us</h1>
</Fragment>
);
};
And, finally, the Home.js file:
import { Fragment } from "react";
export default function About() {
return (
<Fragment>
<h1>Home</h1>
</Fragment>
);
};
I tried to follow a few guides on routing with ReactJS and searching for other answers, but with no success. I don't know what I am doing wrong. Thanks for your time.