score:1
You should only return one component per route and handle authentication within the routed component. Here an example, which handles your Header/Footer case.
function Authenticated ({component: Component, hasFooterHeader, ...rest}) {
const authed = IsLoggedIn();
let page = <>;
if (hasFooterHeader) {
page = (
<>
<Header />
<Shell>
<Component {...rest} />
</Shell>
<Footer />
<>
);
} else {
page = <Component {...rest} />;
}
return (
<Route
{...rest}
render={(props) => authed === true
? page
: <Redirect to={{pathname: '/login', state: {from: props.location}}} />}
/>
)
}
And the Routing would look like this:
<Switch>
<Route path="/login" exact component={Login} />
<Route path="/SignUp" exact component={SignUp} />
<Route path="/ForgotPassword" exact component={ForgotPassword} />
<Route path="/ChangePassword" exact component={ChangePassword} />
<Authenticated path="/startTest" exact component={StartTest} />
<Authenticated path="/TestResult" exact component={TestResult} hasFooterHeader />
<Authenticated path="/TestList" exact component={TestList} hasFooterHeader />
<Authenticated path="/Home" exact component={Home} hasFooterHeader />
<Authenticated path="/" exact component={Home} hasFooterHeader />
<Authenticated component={GenericNotFound} hasFooterHeader />
<Route component={GenericNotFound} />
</Switch>
score:0
When you use react route in app it's important to build clear and extendable code framework
I suggest you seal the Authenticated and Layout under route component
like baselayout.js
in base
then accountlayout
, pagelayout
extend base
then read array of router like [ name :home, layout: pagelayout, auth: authed ]
dynamic load router use js
it's not suitable we Auth
under BrowserRouter
with Switch
you can check umi.js enter link description here
Source: stackoverflow.com
Related Query
- How to render different component with react router after user logged in
- How to 1). put a div , 2). render a component without Route inside <Routes> with React Router in v6 React?
- React Router v6 : How to render multiple component inside and outside a div with the same path
- How to fix "TypeError: Object(...) is not a function" when using a wrap function on React Component to authenticate user login with Browser Router
- How to properly set property(if user is logged) from cookie after page opens or F5 in React with router and redux
- How to render single react component with different data?
- With React Router : How to render a component multiple times with differents props each time in a Route?
- How to render same react component for different routes with different value props?
- How to test the child component render text after the button on the parent component click with react testing library?
- How to render a different component with React Hooks
- How do I pass Redux props to a separate component in a different Route with React Router v4?
- How to pass the match when using render in Route component from react router (v4)
- onEnter Transitions with React Router and Redux Simple Router Dont Render New Route's Component
- How to Get: Component Width After Render in React
- Cannot render same route with different parameters react router v4
- 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 tell if a user is logged in with http only cookies and JWT in react (client-side)
- How can I show a different sidebar based on user information? After logged in. ReactJS
- How to load child component with props on first render with React Hooks
- issue with how to render with react router
- React Redux with redux-observable use the router to navigate to a different page after async action complete
- How to render a React component styled with Emotion in an iframe in Gatsby site?
- How to retrieve the parameter after a hash (#) with React Router and useParams hook?
- How to communicate from Child Component to Parent Component with React Router
- How to properly render Component after this.setState in React
- Send Firebase Auth user to component with React Router
- Using the same component for different Routes with React Router
- rerender a component after redirect with react router
- Keeping user logged in after refresh/using refresh token with Google OAuth2 in React app
- How to make my component re render after updating props from selector in react and redux-saga?
More Query from same tag
- react functional component with ag grid cannot call parent function via context
- Return HTML from react function
- Declaration element with id and access it in same JSX
- Trigger React Router state update
- React UploadCare Remove Button Should Return Null
- how to send null object (image) using axios to django
- Dispatching actions from presenational components with regards to Container Component pattern?
- How to make checkered chess board pattern with CSS
- Docker - served react app, asset-manifest.json with incorrect filenames
- How to fix the error Cannot destructure property temperatureData of 'undefined' or 'null' in react without class
- Pluginable cross-platform software design for react & react native
- Render both text and FontAwesome icon in function return
- How to include meta tags dynamically in react js?
- How to return id with Mongoose Aggregate Lookup with Apollo GraphQL
- Is there any proper way to send a POST request through React using fetch()?
- Updating state item's quantity (React and useReducer)
- Babel Unknown option: .caller
- How override Material UI Popover CSS classes in Select component in React
- Isomorphic Javascript Routes with React-Route vs REST API Routes
- How is this tag being added to the DOM so fast?
- React Router negate path
- How to make React Flexbox stretch to full screen height
- Failed to concat JSON into a JSON Array
- Update value in object in nested array in React Firestore
- Use Microsoft Graph Toolkit with msal-browser
- React router 6, useNavigate how to get pathname
- jsx must wrap with an enclosing tag error
- React - pass array values from arrow function to another .js file
- Running pixel stream in react component?
- react-i18next: interpolation of link in HTML tag in the middle of the text