Route Transition Flickering in React Router Framework v7 (Private Routing) #155444
Replies: 2 comments
-
|
💬 Your Product Feedback Has Been Submitted 🎉 Thank you for taking the time to share your insights with us! Your feedback is invaluable as we build a better GitHub experience for all our users. Here's what you can expect moving forward ⏩
Where to look to see what's shipping 👀
What you can do in the meantime 💻
As a member of the GitHub community, your participation is essential. While we can't promise that every suggestion will be implemented, we want to emphasize that your feedback is instrumental in guiding our decisions and priorities. Thank you once again for your contribution to making GitHub even better! We're grateful for your ongoing support and collaboration in shaping the future of our platform. ⭐ |
Beta Was this translation helpful? Give feedback.
-
|
The flicker happens because you’re redirecting inside useEffect, which runs after the first render. So the protected layout renders briefly before navigation. Instead of using useEffect + navigate, return a immediately: `import { Outlet, Navigate } from "react-router"; const ProtectedRoute = () => { if (!token) { return ; This prevents the protected layout from rendering at all and removes the flicker. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
Bug
Body
I'm currently developing a project utilizing React Router v7 framework where I need to implement protected routes. While I've successfully implemented the protection mechanism, I'm encountering an issue where the protected page's layout briefly flashes before the redirect occurs. I want to prevent this behavior by ensuring the redirect happens before any page rendering takes place. Below are my route.jsx and protected.jsx implementations:
import { useEffect } from "react";
import { Outlet, useNavigate } from "react-router";
import { useDispatch } from "react-redux";
import { getTokenFromCookie } from "./tokenServices";
import { setOpenAuthModal } from "../redux/slices/authSlice";
const ProtectedRoute = () => {
const dispatch = useDispatch();
const navigate = useNavigate();
const token = getTokenFromCookie();
useEffect(() => {
if (!token) {
dispatch(setOpenAuthModal());
navigate("/");
}
}, [token, dispatch, navigate]);
return token ? : null;
};
export default ProtectedRoute;
import { index, layout, prefix, route } from "@react-router/dev/routes";
export default [
layout("./layouts/AppLayout.jsx", [
index("routes/home/route.jsx"),
route("/support", "routes/support/route.jsx"),
route("/rooms/:villaId", "routes/room/route.jsx"),
route("/archive/:cityName", "routes/archive-city/route.jsx"),
]),
// layout("./services/auth/ProtectedRoute.jsx", [
// route("/invoice/reserve/:reserveId", "routes/invoice-reserve/route.jsx"), // ✅ Invoice route here
// ]),
];
Beta Was this translation helpful? Give feedback.
All reactions