Things are changing fast in WEB today, and react-router v6 is in beta already and around the corner. π€
This is just for learning purposes only, r...
For further actions, you may consider blocking this person and/or reporting abuse
Awesome man! Huge thanks π
thanks
I was using a module to route users to I only need to render the header once etc.. so in I have outlet to render the nested routes. How can I now use the with the nested routes. This was my homeModule
And this is how I would want to have it work:
Whoops, I already figured it out, this was the code for my privateoutlet:
Yes π I was about to write that you need to change PrivateOutlet
is it necesary write Outlet twice?
Awesome !
Perfeito, muito obrigado (thank you)! But just aesthetic issues I recommend I created the component that manages private routes receiving props and your code is more reduced and clean.
component={Page} should be capital C component or should at lease be defined:
Great
ΰ€Άΰ€Ύΰ€¨ΰ€¦ΰ€Ύΰ€° ΰ€²ΰ₯ΰ€ ΰ€ΰ€Ύΰ€
Awesome very Good Brother! Many Many Thanks.
You could use
auth-react-router
package npmjs.com/package/auth-react-routerIt provides a really simple API to define your routes and few more configurations (like redirect routes for authorized and unauthorized routes, fallback component for each of the routes)
Just define your routes path and pages component and it will work
Amazing tutorial, thank you β€οΈ
How do you protect a set of routes using PrivateRoute?
Repeating ...
... is not very elegant.
Thank you!
Nice πππ
In this react-router version 6 implementation,how i can show alert or popup message for unlogged user when i trying to navigate private route? if user not logged-in it will show the message "please login first" with login button to navigate login page.
if user not logged-in, user always navigate/redirected to home page or other public route.
See this example, when you do the redirect, you can pass some state
in the login component use that state to show the message
dev.to/iamandrewluca/comment/1jjm9
In this react-router version 6 implementation,how i can show alert or popup message for unlogged user when i trying to navigate private route? if user not logged-in it will show the message "please login first" with login button.
"< L i n k to='/login' > login< L i n k >"
if user not logged-in, user always navigate/redirected to home page or other public route.
In this react-router version 6 implementation,how i can show alert or popup message for unlogged user when i trying to navigate private route? if user not logged-in it will show the message "please login first" with login button.
"< L i n k to='/login' > login< L i n k >"
if user not logged-in, user always navigate/redirected to home page or other public route.
I might of missed something, but is there anything wrong with doing something like this?
I think you should navigate to "/dashboard" (or wherever you want him to go after logging in) instead of "/login".
π€ looking at this it should work, I don't know if
react-router
can detect the full tree of routes using this way π€I can't understand what is 'children' and what it return ?
function PrivateRoute({ children }) {
const auth = useAuth();
return auth ? children : <Navigate to="/login" />;
}
In example bellow
children
insidePrivateRoute
equals<Private />
Ok i understood.Thank you so much.
Could you please share multiple private route example. What your suggestion to keep the code looks clean?
Thank you.
Hey Hasan!
You can put them as adjacent routes
or you can put all private routes in a single parent component that is private
Whan can we use it in create-react-app?
Whenever you want. CRA does not have a router installed by default. But keep in mind it is in beta.
Bro, you are mistaken. I gave it a try to use all the new features of v6 and didn't succeed coz something under the hood used a history.push.
That CRA does not use a router by default I'm very sure. It depends what version of router do you have installed at the moment.
See codesandbox liinks at the end. In that examples I'm using CRA
Your Outlet example doesn't work with react-router-dom v6.0.2 . Any idea why?
Appears to have broken in react-router-dom 6.0.0-beta.6
Updated react-router-dom to last version and updated example.
It seems that you need to pass path to nested routes also, it wasn't the case in earlier versions
it was
now
thanks man
I am getting the duplicate records but my keys are different
Is it correct which I have implemented
PrivateRouter.js
import { Outlet, Navigate } from 'react-router-dom';
const PrivateRouter = (props) => {
const firstLogin = localStorage.getItem('firstLogin')
return firstLogin ? :
};
export default PrivateRouter;
App.js
Can you please review again your post, add more context to the subject, and fix the broken code? Ping me after that, and I'll take a look!
Thank you. Helped a lot.
You are welcome! π€
Thanks a lot man. You are the savior
Hello, Just incase you see this, this is my protected route,
import { Navigate } from "react-router-dom";
import { useSelector } from "react-redux";
import {
selectIsAuthenticated,
selectUserLoading,
} from "../../features/user/userSlice";
import Loader from "../../components/Layout/Loader";
const ProtectedRoute = ({ Component }) => {
const isAuthenticated = useSelector(selectIsAuthenticated);
const loading = useSelector(selectUserLoading);
if (loading === true) {
return (
);
}
return isAuthenticated ? : ;
};
export default ProtectedRoute;
it Works fine but I want to improve it for user experience, when a user hard reloads the page from a browser, I want the user to be returned back to the page it was on, but it gets directed back to the Home page of the application if the user is logged it, but if the user is not logged it, it will go to the login page,
I also feel the glitch might be from my App.js component, which I will paste part of the code below,
function App() {
const dispatch = useDispatch();
const user = useSelector(selectUser);
const seller = useSelector(selectSeller);
const loading = useSelector(selectUserLoading);
const sellerLoading = useSelector(selectSellerLoading);
const allProducts = useSelector(selectAllProducts);
const allEvents = useSelector(selectAllEvents);
const token = localStorage.getItem("token");
const sellerToken = localStorage.getItem("seller_token");
useEffect(() => {
dispatch(getAllProducts());
dispatch(getAllEvents());
}, [dispatch, sellerToken, token]);
return (
<>
Spot how all warpped the protected Route inside my Route,
Kindly reply
How to call this is app.js
import { Outlet, Navigate } from 'react-router-dom';
const PrivateRouter = (props) => {
const firstLogin = localStorage.getItem('firstLogin')
return firstLogin ? :
};
export default PrivateRouter;
Could you please wrap children with fragment in "PrivateRoute" (<>children</>)?
Thank you!
Hi Alexandru! Why would that be needed π€
You use it like this
Both private components are passed inside as children as an array of JSX Elements
[JSX.Element, JSX.Element]
React knows how to render them.
Hi Andrew!
In TypeScript (although this doesn't apply to your case, it can be confusing for beginners), the PrivateRoute component should return a JSX.Element. If the children prop is not wrapped in a React.Fragment (<>...</>), TypeScript will start to show an error:
PrivateRoute cannot be used as a JSX component.
Its return type 'Element | undefined' is not a valid JSX element.
Type 'undefined' is not assignable to type 'Element | null'.
Since a React component should return a JSX element or null, a correct implementation to solve this problem would be to wrap the children in a React.Fragment like this: <> {children} </>.
Hope this helps...
I see. Although this seems to happen only if the component is typed π€
Having a simple function as a component in TS I see no errors. Nevertheless, thanks for the tip, I'm updating the code.
Yes. For me it was a bit confusing given that my components are typed. Thank you!
This article was a lifesaver as I migrate from React Router v5 to v6. Thank you! ποΈ
How can I use this with useRoutes hook provided in react router v6
Ok, but how can I set a state and redirect the users from there they came from
You put some state in
Navigation
props, and on login after submit redirect back, something like thisYou mean to prevent before user is clicking a link? π€ Didn't quite get what exactly do you mean.
Thanks!
Thank you very much....its very helpful
It helped a lot, Thanks buddy!
π€ Glad could help!
It worked! Thank you so much :D