I have a list of pageNames and a list of pageUrls. I would like to create a router that generates a react component for each page in the list and uses a navbar with router links. I think the problem lies in the forEach function but I am not sure.
Can anyone tell what I am doing wrong?
Although I didn't find it helpful, here is a similar question.
import React, { useState } from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
function App() {
const routes = [
{
name: 'home',
path: '/'
},
{
name: 'blog',
path: '/blog'
}
]
// translate (map) your array of objects into jsx
const routeComponents = routes.map(({name, path}) => ((
<Route key={name} path={path}>
<h1>{name}</h1>
</Route>
)))
return (
<div className="App" >
<Router>
<Navbar routes = {routes} />
<Switch>
{routeComponents}
</Switch>
</Router>
</div>
);
}
export default App;
function Navbar(props){
const links = props.routes.map(({name, path}) => (
<Link key={name} to={path}>{name}</Link>
))
return(
<div style={{justifyContent: "space-around", margin:"auto", display:"flex",justifyContent:"space-around",backgroundColor:"red"}}>
{links}
</div>
)
}