score:0
I think you are complicating the route protection a bit. react-router-dom@6
route rendering works quite differently in v6, we don't directly render a Route
in our components, they are to be rendered into the Routes
component or directly nested under other Route
components for route nesting.
I suggest the following implementation:
//v6
import React from "react";
import { Navigate, Outlet } from "react-router-dom";
interface IConditionalRoute = {
condition: boolean;
};
const ConditionalRoute = ({ condition = false }: IConditionalRoute) => {
return condition ? <Outlet /> : <Navigate to={REDIRECT_DESTINATION} />;
};
export { ConditionalRoute };
Note: I'm not super strong in Typescript, so I might not have got the syntax completely correct, but I expect the interface/code should get you close to what you would need.
Usage:
The AdminRoute
would be rendered on what is called a layout route, a route that renders an Outlet
and wraps other Route
components. Omit the path
prop so the path of the nested routes is what is used for matching/rendering.
const AdminRoute: ConditionalRouteProps = () => {
const { isAdmin } = useContext(UserContext);
return <ConditionalRoute condition={isAdmin} />;
};
const TestRoutes: React.FC = () => {
return (
<Routes>
<Route element={<AdminRoute />}>
<Route path="/admin" element={<h1>Admin Page</h1>} />
</Route>
<Route path="/" element={<TestPage />} />
</Routes>
);
};
Source: stackoverflow.com
Related Query
- React/Typescript conditional routing - Updating from React Router Dom v5 to v6
- Nested routing is not working in React Router dom V4 from individual component and works only from App.js component
- React Router - Typescript errors on withRouter after updating version
- TypeScript conditional types: Extract component props type from react component
- React Router DOM History Object Causes Typescript Error
- React Routing with Express, Webpack dev middleware, React router dom
- How can I solve this problem. React Router not updating routing on hosting
- router props and custom props with typescript react router dom for functional components
- React router dom passing data from parent component to child router component does not pass the props.match
- How to add Link from react router dom to the submit button with the default Submit validation
- React Router is able to do the routing only if started from root url
- React router exact matching only updating URL without re-render of DOM
- Convert from react router dom v5 to react router dom v6 when using useRouteMatch
- Updating React 17 to React 18 - Typescript - Children of ReactNode type no longer handles inline conditional rendering of `void`
- How to get props from <Route /> with react router dom v6
- React router dom routing to ItemDetail
- React Router Dom v6 useNavigate to Conditional Path Directory After Login
- Conditional Routes in react router dom v6
- Protect routes with react router dom v6 Nothing was returned from render
- react router dom is not updating class component when url change
- React Router Dom v6 with Typescript
- react router dom flickers to fallout router while fetching data from server - react 6
- Error in upgrading code from v5 to v6 in react router dom
- External link not working when using LINK from react router dom
- Conditional Routing and Conditional Rendering in ReactJS and React router
- React Router dom not routing
- React router dom dynamic routing
- How to put Link from react router dom inside Material UI table
- Remove trailling slash from base url in react router dom
- Display URL params on page when using react `withRouter` from react router dom
More Query from same tag
- React: pass a query string into a path
- Is it possible to pass props by match in React Link
- Using usetState in ReactJs with different values
- Show one specific result in a list in React
- reactjs select radio input based on the props
- React complex filter with map
- React -- prevent input/textarea from focus on render
- How to add new route dynamically?
- How to display time with momentjs in react
- Should I define standard React PropTypes on my component?
- Slate: How to find when Editor has fully rendered
- react got error of unknown props
- Patterns in React (wrapper)
- sequencing async/await calls in TypeScript
- How to filter an array and add values to a state
- React change style on function call
- map function in React app showing only one result
- Invalid hook call. Hooks can only be called inside of the body of a function component.?
- TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. when trying to append react element
- QuotaExceededError: The quota has been exceeded - progressive web app offline mode
- Reload after deleting user from end of list with React
- Equivalent of document.querySelectorAll() in React, onScroll
- How to get id from params in reactjs in a class component?
- How to configure correctly Firebase and get data in Firestore
- I Need to fix a issue in antd tree component
- Is it possible to create a generic React list component with typescript?
- Enum React multiple different child components
- How to show components conditionally based on route
- The main components of container look broken while visiting other sub-project pages in Micro-frontend
- How to redirect to home in react router dom after login with redux?