This is code for looping through routes file. Here i am creating routerconfig array an exporting from here to app.js.
import React, { Component } from 'react'; import { Route } from 'react-router-dom'; import routes from './routes'; class RouterConfig extends Component { render() { return ( routes.map((route) => { return ( <Route key={ route.id } path={ route.path } exact={ route.exact } component={ route.component } /> ); }) ); } } export default RouterConfig;
This is my route configuration file, where i have listed all routes.
import Home from '../components/home/home'; import About from '../components/about/about'; import Posts from '../components/posts/posts'; import NotFound from '../components/not_found/not_found'; const routes = [ { path: '/', exact: true, component: Home, id: 'home', }, { path: '/about', component: About, id: 'about', }, { path: '/posts', component: Posts, id: 'posts', }, { component: NotFound, id: 'not_found', }, ]; export default routes;
This is my app.js import React, { Component } from 'react'; import { Switch, Router, Route } from 'react-router-dom'; import Header from '../header/header'; import Footer from '../footer/footer'; import history from '../../history'; import RouterConfig from '../../router/browserroute';
class App extends Component { render() { return ( <div> <Router history={ history } > <div> <Header /> <Switch> <RouterConfig /> </Switch> <Footer /> </div> </Router> </div> ); } } export default App;
issue is on every page i am getting not found component render. Even using switch with routes not getting the expected results. not sure why code is not working properly.
[this is how it renders not found in every page ][1]
[1]: https://i.stack.imgur.com/B7evW.png
on changing:
class App extends Component { render() { return ( <div> <Router history={ history } > <div> <Header /> <Switch> <Route exact path='/' component={ Home } /> <Route path='/about' component={ About } /> <Route path='/posts' component={ Posts } /> <Route component={ NotFound } /> </Switch> <Footer /> </div> </Router> </div> ); } }
This works fine
RouterConfig
does not throw an error when arender
attempt is made. Is this a detail you omitted? – Andrew