I'm working on a React web app to route different pages. Using React Router
Requirement: Totally 2 major parent routes namely (Customer and Admin) So when user hits ex.: localhost:3000 it should default to homepage (Customer Component) and localhost:3000/admin route to (Admin Page).
Problem: Either I'm only able to route all child components of Customer (or) Admin. Both are not working as properly.
Currently What is Working?
- localhost:3000 - Loading Home page
- localhost:3000/sale - Blank page (need to fix)
- localhost:3000/sale/product - Blank page (need to fix)
- localhost:3000/admin - Loading Admin page
- localhost:3000/admin/all-products - Loading Product List page
- localhost:3000/admin/add-product - Loading Product Detail page
Files: Index.js
ReactDOM.render(
<BrowserRouter basename='/'>
<App />
</BrowserRouter>,
document.getElementById("root")
);
App.js
class App extends Component {
render() {
return (
<Switch>
<Route exact path='/' component={Customer} />
<Route path='/admin' component={Admin} />
</Switch>
);
}
}
Customer.js
class Customer extends Component {
render() {
return (
<div>
<Header />
<div className='container mainContent'>
<Breadcrumbs />
<Switch>
<Route path='/' component={Home} />
<Route path='/sale' component={ProductList} />
<Route path='/sale/product' component={ProductDetails} />
</Switch>
</div>
<Footer />
</div>
);
}
}
Admin.js
class Admin extends Component {
render() {
return (
<div className='container-fluid'>
<div className='row'>
<ul className='sidebar-submenu '>
<li>
<NavLink to='/admin/all-products' activeClassName='active'>All Products</NavLink>
</li>
<li>
<NavLink to='/admin/add-product' activeClassName='active'>>Create Product</NavLink>
</li>
</ul>
<div className='col-md-10 admin-content'>
<Switch>
<Route path='/admin/all-products' component={ProductListView} />
<Route path='/admin/add-product' component={ProductListCreate} />
</Switch>
</div>
</div>
</div>
);
}
}