score:1
Accepted answer
here what i've done. see demo.
i don't wanna be too confused because of this, so i choose a simple way.
routes.js
import home from "./pages/home";
import complexpath from "./pages/complexpath";
import login from "./pages/login";
export default [
{
path: "/",
component: home,
withheadersidenav: true
},
{
path: "/yet/another/complex/path",
component: complexpath,
withheadersidenav: true
},
{
path: "/login",
component: login,
withheadersidenav: false
}
];
then, simply map
the routes.
app.js
import react from "react";
import { switch, route } from "react-router-dom";
import baselayout from "./baselayout";
import routes from "./routes";
export default class extends react.component {
state = {
withheadersidenav: true
};
showheadersidenav = (withheadersidenav = true) => {
this.setstate({ withheadersidenav });
};
render() {
return (
<baselayout withheadersidenav={this.state.withheadersidenav}>
<switch>
{routes.map(route => (
<route
exact
key={route.path}
path={route.path}
render={() => (
<route.component
showheadersidenav={() =>
this.showheadersidenav(route.withheadersidenav)
}
/>
)}
/>
))}
</switch>
</baselayout>
);
}
}
there will be a hoc for each page to handle layout changing. see pages/withbase.js
in demo project.
Source: stackoverflow.com
Related Query
- How to add Nested and Non-Nested Routes with React Router - ReactJS?
- How to add login authentication and session to ReactJS React Router and Redux with MongoDB + NodeJS Express?
- How to Add Nested Routes in ReactJS - React Router
- How to make react router work with static assets, html5 mode, history API and nested routes?
- React router 4 how to nested routes /admin and /
- Framer Motion and React router 5: How do I prevent re-render of parent components with nested routes?
- How to route correctly with a mixture of parent and child routes with React Router
- react router 4 - how to make a root path with nested routes but fallback to not-found
- React Router v6 - how to handle nested routes but with only current page showing instead of previous pages
- How to make redirect in nested routes with React Router
- Building a dynamic nested routes architechure with React and React Router
- How to add react router Error together with private routes
- How to do nested routes with react router 4?
- Nested Match routes with React Router v4 and MatchWithFade
- Nested routes with react router v4 / v5
- How to test a component with a nested container with React and Redux?
- How to use Apollo Client + React Router to implement private routes and redirection based on user status?
- How to navigate to another page with a smooth scroll on a specific id with react router and react scroll
- React Router v4 Nested Routes with Switch
- React Router doesn't load images properly with nested routes
- How to link to nested routes in React Router
- React router nested routes - How to redirect when no route is matching
- How to nest Scene and navigate with direction='vertical' in React Native Router Flux?
- React Router V4: How to render a modal in the same screen changing only the url and then with that url, be able to rebuild the whole screen
- How to test React Router params with Redux and enzyme
- React Router v4 nested routes not work with webpack-dev-server
- How to use nested routes to add content to a page without removing the content of the previous route with react-router-v4?
- React router v4 with redux protected routes and auth
- How to retrieve the parameter after a hash (#) with React Router and useParams hook?
- Nested Routes not rendering with React Router v4
More Query from same tag
- Webpack 5 - Uncaught ReferenceError: process is not defined
- How to change the state of the particular component when it is clicked, using useState in react js?
- Functional component as initial state value throws children undefined without wrapping in React.memo
- how can i change the status of the username in reactjs using axios?
- react-mapbox-gl _onClickGeolocate() call with setTimeout to request location automatically
- how to append drop down options inside render function?
- Which is the proper way to wait for an API call to complete before calling a function
- Taking the last five digits of a number from user input in REACT.jsx
- using styled-component in react.js
- Render JSON data (from reddit API) with reactjs
- Component with specific child aggregations in React / Typescript
- Input element not accepting capital letters
- Implement React-pagination with Rest Api
- React JSX conditional rendering breaks dropdown
- How to redirect to log in page after click logout button on navbar header in React?
- CKEditorError: ckeditor-duplicated-modules: Some CKEditor 5 modules are duplicated
- How to import css file for into Component .jsx file
- How to test Ajax call using Jest?
- Axios: onUploadProgress event gets only triggered after video is uploaded completely
- useCallback with args passed in
- Handling an onClick on a component in React
- Add entries dynamically
- Why does JSON.parse throw a cross-origin error?
- How to fetch form with file and recieve it on net core?
- JSX return a html segment
- fetch Data when context changes in React
- React state, setState with const
- Why setState of react-select's menu selection making modal close state?
- React Hooks: memoize click handlers for list?
- Two different subdomains in one Next.js routing