React Router 4 pretty much solves this as it made the route configuration part of the component rendering, so having conditional rendering is the same whether it's based on the location or on other props/state.
The closest thing to a clean "React-Router-ish" way to do that is to use the React Router Enterhooks.
An enter hook is a user-defined function that is called when a route is about to be rendered. It receives the next router state as its first argument. The replace function may be used to trigger a transition to a different URL.
So, use the onEnter
(nextState, replace, callback?) attribute on your
Called when a route is about to be entered. It provides the next router state and a function to redirect to another path.
thiswill be the route instance that triggered the hook.
If callback is listed as a 3rd argument, this hook will run asynchronously, and the transition will block until callback is called.
The general best practice I follow is to place the auth-check flow away from your routes, and place it inside the transition events/hooks.
The usual behavior is - before the route handler actually gets rendered, check the auth, and redirect the user to another route. In your case, if you want to use the same route, but render different components - you should be able to do that using the same technique too. However, that's not a common thing (based on what I've seen), but it should be possible.
For a complete example of this approach, here's the auth-flow code example you can check. It is shared by the creators of React Router, so it looks credible to me.
PS: My answer is valid for React Router versions > 0.13.x.
- Is there a clean way to conditionally load and render different components for the same React Router route?
- Using for loops and switch cases in React to dynamically render different components
- Is there a way to make separate files for components such as logic, html and CSS in ReactJS like Angular?
- How can I conditionally render different components on initial load in React?
- Is there a way to count the products according to a specific category without making different pagination components for the categories?
- How to conditionally render a dialog and drawer in material UI for different screen resolutions?
- React: How to render same page that includes multiple components and forms for 2 types of user with different permissions?
- Is there a way to render multiple React components in the React.render() function?
- Is there a way to specify minimum and maximum zoom level for a browser?
- Render different components on client and server side
- Save components to a list and then conditionally render
- Load different JS library files for different components
- Expand row by column : Expand row by multiple columns and load different components according to the column clicked
- How to conditionally render a DIV and REACT component for OPENING and CLOSING tags?
- Material-UI React.js, Is there a way to have an onClick event for both a ListItem, and the ListItem's Avatar?
- Is there any way to set a background colour to the full column for Saturday and Sunday in 'React Full Calendar'
- What is the best way to apply layout styling to styled-components and React components used in different places on a site?
- ReactJs/NextJs - Conditionally render mutually exclusive components without compromising load time
- Components flicker during server side render for nextjs and material-ui?
- Is there a way to apply CSS styling to lazy-loaded images before they fully load with Gatsby and Sharp?
- Approach for different HTML structure with React and using the same components
- Is there a way to use styled components for ANT notifications?
- What's the correct way to render React components with different styles such as Navbar using React Router 6v?
- Is there a better way to conditionally render these BE responses on the FE? using react
- Dispatching and listeing for events in different components - reactjs
- Is there a way to load react library files separately and not in the bundled files when using react with babel, webpack and gulp as building tool?
- React Router: How to render different components with the same route conditionally
- What is the best way to render multiple button components with different values?
- Using React Hooks, using "useState" and "useEffect", is there a way to render a state waiting till the state has a value?
- React and Material UI, is there is a clean way to de-structure props that I am sending to useStyles
More Query from same tag
- How to implement Auth Code Flow with Okta
- React TS Variable is not updated in time
- Unable to add dynamic style in styled component. I am getting type error
- How to apply styling to MapView when using @arcgis/core for imports?
- JS Filter/Map to get objects between
- How to callback a function on onClick event in ReactJs
- Dynamically insert script tag that will execute via Reactjs
- React Hooks useState Event Handler With Styled Components
- React: Is it ok to check cookies for refresh token?
- Getting a console message : Failed prop type: The prop `value` is marked as required in `Rating`, but its value is `undefined`
- ReactJS Interest-cohort
- update reactjs context after ajax request finished with flux architecture
- Why does my JSON response crash on forEach? With TypeError undefined
- Remove the arrow and cross that appears for TextField type=“time” material-ui React
- Browser's progress bar not visible when I click on a link on my next.js (react) app
- How in useState iterate over an array
- how to pass multiple parameters to the web API?
- React POST form Fetch but Undefined data
- Smooth wrapper background-image transition, when using React onMouseEnter and onMouseLeave events on a childs
- Validation is not working for these and unable to write something in the forum
- How can I update the state of my container component from one of its children in React?
- Can you interpolate path using react-router-dom v6 - ReactJS
- Destructuring assignment via TypeScript in React
- Using handleChange function for several inputs
- Why do I get 401 for static fies when using Express with React.js?
- Transpiling next.js server code
- Declaring routes using an array
- Compare array of object values if equals add it total
- Loading translations from multiple backends
- Gatsby-ssr and React context crash