score:57
yes, daniel is correct, but to expand upon his answer, your primary app component would need to have a navbar component within it. that way, when you render the primary app (any page under the '/' path), it would also display the navbar. i am guessing that you wouldn't want your login page to display the navbar, so that shouldn't be a nested component, and should instead be by itself. so your routes would end up looking something like this:
<router>
<route path="/" component={app}>
<route path="page1" component={page1} />
<route path="page2" component={page2} />
</route>
<route path="/login" component={login} />
</router>
and the other components would look something like this:
var navbar = react.createclass({
render() {
return (
<div>
<ul>
<a onclick={() => history.push('page1') }>page 1</a>
<a onclick={() => history.push('page2') }>page 2</a>
</ul>
</div>
)
}
});
var app = react.createclass({
render() {
return (
<div>
<navbar />
<div>other content</div>
{this.props.children}
</div>
)
}
});
score:1
for @chris 's answer to work in a functional component. here is the answer react-router, router-dom versions v5.2.0
import react from 'react';
import { link, switch, route } from 'react-router-dom';
import header from './header';
import footer from '.footer';
import page1 from './page1';
import page2 from './page2';
import page3 from './page3';
import loading from './loading'; // a page to show spinner
const dashboard = react.lazy(() => import('../pages/dashboard'));
// if you have an admin dashboard to load lazily
const navroute = ({exact, path, component: component}) => (
<route exact={exact} path={path} render={(props) => (
<div>
<header/>
<component {...props}/>
<footer/>
</div>
)}/>
)
function app() {
return (
<div>
<suspense fallback={<loading />}>
<switch>
<navroute exact path="/" component={landing} />
<route exact path="/admin/dashboard" component={dashboard}/>
<route exact path="/" component={login} />
<navroute exact path="/path1" component={page1} />
<navroute exact path="/path2" component={page2} />
<navroute component={page404} />
</switch>
</suspense>
</div>
);
}
export default app;
score:8
react router version 6+ 2021 routes with/without navbars
import { outlet, route, routes } from "react-router-dom";
// your components here
const app = () => {
return (
<>
<routes>
{/* routes that needs a navbar will need to go as children of this route component */}
<route path="/" element={<layoutswithnavbar />}>
<route path="/" element={<div>home screen</div>} />
<route path="/welcome" element={<welcome />} />
<route path="/something" element={<somethinggg />} />
<route path="/somethingprotected" element={<yourcustomprotectedroute component={someone}/>} />
<route path="/something/:id" element={<protectedroute id component={somethingbyid}/>} />
</route>
{/* routes without a navbar you can add them here as normal routes */}
<route
path="/idontneednavbar"
element={<protectedroute component={providerregistrationinfo} />}
/>
</routes>
</>
);
function layoutswithnavbar() {
return (
<>
{/* your navbar component */}
<navbar />
{/* this outlet is the place in which react-router will render your components that you need with the navbar */}
<outlet />
{/* you can add a footer to get fancy in here :) */}
</>
);
}
how it works
the layoutswithnavbar
component will render the navbar with any component you need with a navbar. you can find an example in the official react-router v6 docs here: https://reactrouter.com/docs/en/v6/getting-started/overview#nested-routes
learn more about the outlet
component: https://reactrouter.com/docs/en/v6/getting-started/concepts#outlets
score:74
note the accepted is perfectly fine - but wanted to add a version4 example because they are different enough.
nav.js
import react from 'react';
import { link } from 'react-router';
export default class nav extends react.component {
render() {
return (
<nav classname="nav">
<div classname="nav__container">
<link to="/" classname="nav__brand">
<img src="logo.svg" classname="nav__logo" />
</link>
<div classname="nav__right">
<ul classname="nav__item-wrapper">
<li classname="nav__item">
<link classname="nav__link" to="/path1">link 1</link>
</li>
<li classname="nav__item">
<link classname="nav__link" to="/path2">link 2</link>
</li>
<li classname="nav__item">
<link classname="nav__link" to="/path3">link 3</link>
</li>
</ul>
</div>
</div>
</nav>
);
}
}
app.js
import react from 'react';
import { link, switch, route } from 'react-router';
import nav from './nav';
import page1 from './page1';
import page2 from './page2';
import page3 from './page3';
export default class app extends react.component {
render() {
return (
<div classname="app">
<router>
<div>
<nav />
<switch>
<route exactly component={landing} pattern="/" />
<route exactly component={page1} pattern="/path1" />
<route exactly component={page2} pattern="/path2" />
<route exactly component={page3} pattern="/path3" />
<route component={page404} />
</switch>
</div>
</router>
</div>
);
}
}
alternatively, if you want a more dynamic nav, you can look at the excellent v4 docs: https://reacttraining.com/react-router/web/example/sidebar
edit
a few people have asked about a page without the nav, such as a login page. i typically approach it with a wrapper route component
import react from 'react';
import { link, switch, route } from 'react-router';
import nav from './nav';
import page1 from './page1';
import page2 from './page2';
import page3 from './page3';
const navroute = ({exact, path, component: component}) => (
<route exact={exact} path={path} render={(props) => (
<div>
<header/>
<component {...props}/>
</div>
)}/>
)
export default class app extends react.component {
render() {
return (
<div classname="app">
<router>
<switch>
<navroute exactly component={landing} pattern="/" />
<route exactly component={login} pattern="/login" />
<navroute exactly component={page1} pattern="/path1" />
<navroute exactly component={page2} pattern="/path2" />
<navroute component={page404} />
</switch>
</router>
</div>
);
}
}
Source: stackoverflow.com
Related Query
- React router nav bar example
- Loading nav bar in react router switch
- React Router v4 Animated Transition Example
- React Router DOM — How to include a navigation bar on every page?
- How to hide Nav bar in some react components
- react router app hosted on firebase issues 404 error only when trying to access route via address bar
- React navigation bar is in the screen even though it is not on the router
- React Bulma mobile nav bar is not working as expected
- React Redux Loading bar for react router navigation
- different nav bar for different pages in react
- Hide/Unhide toggle of nav links across different Routes in React Router V4
- React Router loads URL in address bar but doesn't load component
- Elements are going inside the nav bar css react
- How do I get the active parameter value from React Router in my sidebar or nav to create a link?
- react router 4 query parameters example not working
- Basic React Router Example not working
- React / CSS - float nav bar right with flex boxes?
- React router provides javascript object instead of original type on url address bar reload
- React js component on router / header footer nav on switch
- Update document and app bar title when using React Router
- React router returning blank page when accessed directly from browser address bar
- Can't get React Router to work with this simple example
- How can I have active nav link in React with React Router Dom but just on big screens until my breakpoints kick in?
- React Router - The Address Bar Is Changing But Not Rendering The Component
- React router dom NavLink only works when refresh no when click the nav item
- React Router 5 - How to define the route for specific page without header and side navigation bar
- nav disappearing components using react hooks and react router
- React Bootstrap Nav bar Home tab always appears to be highlighted
- How do I open a modal/ dialog from a button from nav bar component in every page react
- React SlidingPane header getting hidden under Nav bar
More Query from same tag
- React: How to call multiple state-changing functions, in one procedure?
- How do I make it so that my app remembers the current route on page refresh (using react-router and express)
- React/Redux handling API server errors in reducer to display on UI
- How can I set a class to the parent element if the child contains certain class?
- How to catch the correct return of useeffect in a component from an array of objects?
- NodeJS events triggering multiple times in electron-react app
- Jest unable to mock a function inside FC
- Dynamically importing css files into react
- How to open Drawer from another component
- How to animate change of state made by React this.setState({})?
- Nested API calls in React.js
- React Router 4, how to check for a possible redirect on all routes?
- How to set new Sequence of the index while creating a new row in a table?
- In React, OK to always call ReactDOM.hydrate instead of ReactDOM.render?
- Cannot handle error in React ApolloClient
- Can one conditionally import a module in NodeJS/React?
- React Js Error Can't perform a React state update on an unmounted component
- React Table set Header onClick-Function without disable sort-Function
- Reactjs. Counter of renders
- Is there a way of maintaining a lot objects in react state wiesly
- Redux: How to update one value upon another changing?
- Multiple RegEx not working with conditions
- Insert CSS line in React code
- React-router v4 on server side, how to match request url to jsx routes?
- How to update the input value when catching an onChange event in React?
- spying a function called in a rejected promise
- Using useEffect with React-redux
- ag-grid webpack css not making to the website
- Search result pagination with React JS
- how to use react number format with formik