I am trying to build a personal resume website but react router is not rendering when I call my components in my App.js
routes. The component renders fine when I call it above my routes but I do not want it to be displayed to all my pages, I just want it to be rendered to the home route. I would appreciate any help. Thank you in advance.
Home.js
import React from 'react';
import '../../App.css';
import HeroSection from '../HeroSection';
function Home() {
return (
<div>
<HeroSection />
</div>
);
}
export default Home;
App.js
import React from 'react';
import './App.css';
import NavBar from './Components/NavBar';
import {BrowserRouter as Router, Routes, Route} from 'react-router-dom';
import Home from './Components/pages/Home';
import Projects from './Components/pages/Projects';
function App() {
return (
<Router>
{/* put anything that should be displayed to all pages */}
<NavBar/>
<Home/> {/*works fine here*/}
<Routes>
<Route exact path= './' component={Home}/>
</Routes>
</Router>
);
}
export default App;
package.json
{
"name": "myresume",
"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:
Your using react-router-dom version 6!
Thats NOT how you do it in version 6. Read the docs!
It should be element={<Home />}
not component={Home}
You can see the bascis here https://reactrouter.com/docs/en/v6/getting-started/overview
On how to upgrade from v5 to v6: https://reactrouter.com/docs/en/v6/upgrading/v5
CodePudding user response:
Can you try the below method instead?
import Home from './Components/pages/Home';
function App() {
return (
<Router>
<NavBar/>
<Routes>
<Route exact path= '/' element={<Home/>}/> // replace component with element
</Routes>
</Router>
);
}
I'm basing my answer off of this since you are using version 6 of react router dom https://reactrouter.com/docs/en/v6/getting-started/overview
CodePudding user response:
Maybe you can try:
<Route exact path="/" element={<Home />} />