I need to navigate back to the original requested URL after login.
For example, user enters www.example.com/settings
as user is not authenticated, it will navigate to login page www.example.com/login
.
Once authenticated, it should navigate back to www.example.com/settings
automatically.
My original approach with react-router-dom
v5 is quite simple:
const PrivateRoute = ({ isLoggedIn, component: Component, ...rest }) => {
return (
<Route
{...rest}
render={(props) =>
isLoggedIn? (
<Component {...props} />
) : (
<Redirect
to={{ pathname: `/login/${props.location.search}`, state: { from: props.location } }}
/>
)
}
/>
);
};
<PrivateRoute exact isLoggedIn={isLoggedIn} path="/settings" component={Settings} />
Can some one tell me how to do that in v6? Thanks in advance
The Redirect component was usually used in previous versions of the react-router-dom package to quickly do redirects by simply importing the component from react-router-dom and then making use of the component by providing the to prop, passing the page you desire to redirect to.
Rendering a <Redirect> will navigate to a new location. The new location will override the current location in the history stack, like server-side redirects (HTTP 3xx) do.
Our recommendation for redirecting in React Router v6 really doesn't have much to do with React or React Router at all. It is simply this: if you need to redirect, do it on the server before you render any React and send the right status code. That's it.
React route redirect example; React router DOM is an additional package used for setting up routing in React environment. Router dom handles plenty of routing-related tasks through its powerful api mechanism.
Create a new react application in your system using the provided command: The primary thing is to do when working with routing in react is to install the react router dom package. Open the project, make the components/ folder, make the User.js file and write the provided code into the file:
The primary thing is to do when working with routing in react is to install the react router dom package. Open the project, make the components/ folder, make the User.js file and write the provided code into the file: In the src/ directory you need to create the components/ directory.
In react-router-dom
v6 rendering routes and handling redirects is quite different than in v5. Gone are custom route components, they are replaced with a wrapper component pattern.
v5 - Custom Route
Takes props and conditionally renders a Route
component with the route props passed through or a Redirect
component with route state holding the current location
.
const CustomRoute = ({ isLoggedIn, ...props }) => {
const location = useLocation();
return isLoggedIn? (
<Route {...props} />
) : (
<Redirect
to={{
pathname: `/login/${location.search}`,
state: { location },
}}
/>
);
};
...
<PrivateRoute
exact
isLoggedIn={isLoggedIn}
path="/settings"
component={Settings}
/>
v6 - Custom Wrapper
Takes props and conditionally renders an Outlet
component for nested Route
components to be rendered into or a Navigate
component with route state holding the current location
.
const CustomWrapper = ({ isLoggedIn, ...props }) => {
const location = useLocation();
return isLoggedIn? (
<Outlet />
) : (
<Navigate
to={`/login/${location.search}`}
replace
state={{ location }}
/>
)
};
...
<Route path="settings" element={<CustomWrapper isLoggedIn={isLoggedIn} />} >
<Route path="settings" element={<Settings />} />
</Route>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With