I'm new to learning React so I'm wondering if anyone can help me. I'm currently building a portfolio through React and I'm working on the Nav for this. I'm not getting any errors in the Console and when I click on each Link, it displays the Link I've clicked on in the URL but it won't show me any of my Content from each Link. Can anyone please help explain where I am going wrong?
App Code
import React from 'react';
import { BrowserRouter, Route, Link } from 'react-router-dom';
import Projects from './Projects.js';
import Contact from './Contact.js';
import About from './About.js';
import Skills from './Skills.js';
import Home from './App.js';
import './App.css';
function App() {
return (
<BrowserRouter>
<div className="App">
<Route path="/" element={<Home />}></Route>
<Route path="/about" element={<About />}></Route>
<Route path="/contact" element={<Contact />}></Route>
<Route path="/projects" element={<Projects />}></Route>
<Route path="/skills" element={<Skills />}></Route>
<div className="navigation">
<div className="navigation-sub">
<Link to="/" className="item">Home</Link>
<Link to="/about" className="item">About</Link>
<Link to="/contact" className="item">Contact</Link>
<Link to="/projects" className="item">Projects</Link>
<Link to="/skills" className="item">Skills</Link>
</div>
</div>
</div>
</BrowserRouter>
);
}
export default App;
index Code
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>,
document.getElementById("root")
);
reportWebVitals();
About Code
import React from "react";
function About(props) {
return (
<div>
<h1>About Me</h1>
</div>
)
}
export default About;
Thanks :)
You are already rendering a BrowserRouter around App, so remove the extraneous BrowserRouter from App. The Route components are also missing being wrapped in a Routes component that handles path matching. It's a required component. You should have some console errors warning of invariant violations for the rendering of a router within another router and the routes not being wrapped directly by a Routes component.
import { Routes, Route } from 'react-router-dom';
function App() {
return (
<div className="App">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
<Route path="/projects" element={<Projects />} />
<Route path="/skills" element={<Skills />} />
</Routes>
<div className="navigation">
<div className="navigation-sub">
<Link to="/" className="item">Home</Link>
<Link to="/about" className="item">About</Link>
<Link to="/contact" className="item">Contact</Link>
<Link to="/projects" className="item">Projects</Link>
<Link to="/skills" className="item">Skills</Link>
</div>
</div>
</div>
);
}