score:3
Accepted answer
<Route path='/' component={Home} onEnter={requireAuth}>
function requireAuth(nextState, replace) {
if (!role) {
replace({
pathname: '/login',
state: { nextPathname: nextState.location.pathname }
})
}
}
score:2
<Route path="/" component={role===undefined ? Login : Home} />
Source: stackoverflow.com
Related Query
- React Router - Auth based redirecting
- Changing components based on url with react router
- Refresh on protected Routes React Router with firebase auth
- React router v4 with redux protected routes and auth
- Send Firebase Auth user to component with React Router
- Cannot test redirecting in react router with enzyme
- React Typescript: Configure router to allow username based URL's without conflicting with other routes
- React Router trigger re-load / re-mount component on redirecting to same page with different slug
- Server-Side Auth Flow with React Router
- Auth based redirecting with react-router
- Protected Routes React Router 4 not working with auth state stored in Redux
- Routing based on different environments with React Router
- React router protected route with firebase auth check
- With React Router v4, how to navigate to a path based upon the existing browser location but with new params interpolated?
- Redirecting with react router
- Dynamic views based on url ID with React Router V6
- React middleware auth component with react router
- Redirecting programatically using React Router with Flux
- Add param to current URL without reloading or redirecting with react router
- How to render components dynamically based on route name with React router
- How to setup dynamic routing with React Router based on props received?
- React+Laravel: Nivo Slider does not works properly with React Router based navigation
- Having problems with React Router Auth redirection
- React Router with optional path parameter
- Nested routes with react router v4 / v5
- Multiple params with React Router
- Material-UI's Tabs integration with react router 4?
- How to use React Router with Electron?
- How to fetch the new data in response to React Router change with Redux?
- React Router v4 with multiple layouts
More Query from same tag
- React Router 1.0 - replace # and get rid of ?_k
- React output JSON on new line
- How to change Header and footer of a specific webpage while using react route
- Showing Real Time Variable Value In <Text> Component - React Native
- Creating universal React components
- Breakpoints not working at set breakpoints
- How to make button color change if button selected and unselected
- How to test react-toastify with jest and react-testing-library
- fetch function doesn't called with react-redux
- React-Redux issue with form submission (Server error 500)
- React conditional Show/hide not working after input to form
- how to create custom key bindings (Ctrl+Enter) in draft js
- RXJS observables cancel mouseover stream after mouseout event
- Trouble getting React working with webpack-dev-server
- How to use the key attribute do in an img tag?
- react re render component based on api fetch
- ReactJS - Warning: A component is changing an uncontrolled input of type text to be controlled - edit function
- Styled components using pseudo element :checked
- Hide and Show modal on mouseenter and mouseleave using React Hooks
- Sibling Functional Components communication in reactjs
- Having trouble using the react-masonry-component
- React does not recognize the prop passed to a styled-component within Material UI
- Is there any way to show the Select MenuItems from specific position at first dropdown?
- How do I add an anchor tag to a string prop?
- Overriding add row function in material table
- How to pass useState state to another component?
- Custom contents <td> in ReactJS
- Electron: how to open an url in the same window and after the user do some action go back to application in the same window
- find variant id based on attributes selection
- Attempted import error: 'Navlink' is not exported from 'react-router-dom'