I have installed 'react-router-dom' and 'react', and am trying to implement a routing feature. I have also added 'react-router-dom' to my devDependencies within the package.json. However, the issue is that the routing feature is not working as expected.
Here's the code to the Navbar component with some slight edit (which is copied directly from bootstrap https://getbootstrap.com/docs/5.3/components/navbar/) which implements this feature:
import { Link } from 'react-router-dom';
const Navbar= () => {
return (
<nav className="navbar navbar-expand-lg bg-body-tertiary">
<div className="container-fluid">
<Link className="navbar-brand" to="/">Movie Browser</Link>
<button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav me-auto mb-2 mb-lg-0">
<li className="nav-item">
<Link className="nav-link active" aria-current="page" to="/">Home</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/about">About</Link>
</li>
<li className="nav-item">
<Link className="nav-link disabled">Coming soon</Link>
</li>
</ul>
<form className="d-flex" role="search">
<input className="form-control me-2" type="search" placeholder="Search" aria-label="Search" />
<button className="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
);
}
export default Navbar;
Based on the code above, clicking on the equivalent tags in the webpage should load up another path I have created, i.e. either "/" or "/about".
However, clicking does not work as the webpage does not render anything new and sticks to the same content, i.e. the component. However, the url on top of the webpage does change accordingly. Only when I manually enter the url does the webpage start rendering properly to the component.
I'd like to seek advice on what is the issue here.
Here's also the code within the default App.js that uses the Navbar component:
import './App.css';
import Navbar from './components/Navbar';
import Home from './components/Home';
import AboutView from './components/AboutView';
import { Switch, Route } from "react-router-dom";
function App() {
return (
<div>
<Navbar />
<Switch>
<Route path="/" exact>
<Home />
</Route>
<Route path="/about" component={AboutView}/>
</Switch>
</div>
);
}
export default App;
Here's the code within the default index.js:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from 'react-router-dom';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
reportWebVitals();
Below is an image of the issue I am facing:
But the intended effect should be:
Here are the versions for my react and react-router-dom:
Thank you.