#React-Router redirect not working

9 messages · Page 1 of 1 (latest)

glass pawn
#

I finished the React-Router course and am trying to build a custom app with what i learned
the problem is with the requireAuth redirect it is supposed to redirect to login if NOT logged in but i am just getting an error : "import { redirect } from "react-router-dom";

export async function requireAuth() {
const isLoggedIn = false;

if (!isLoggedIn) {
window.location.href = "/login";
}
}
" when i change to window.location.href it does work "import { redirect } from "react-router-dom";

export async function requireAuth() {
const isLoggedIn = false;

if (!isLoggedIn) {
window.location.href = "/login";
}
}"

heres my full code https://codesandbox.io/s/thirsty-colden-hc2dj5?file=/src/App.js

#

this is my error: Unexpected Application Error!
{"type":"default","status":302,"ok":false,"statusText":"","headers":{"map":{"location":"/login"}},"url":"","bodyUsed":false,"_bodyInit":null,"_bodyText":""}

sonic sequoia
#

That's actually a clever solution! Never thought about using window.location. By the error, it looks like you are using miragejs. They use a library that "monkey-patches" the response object which is what redirect uses. This link https://discord.com/channels/684009642984341525/1089513873029201941 provides a workaround for it if you want to use the latest version of the router with redirect, otherwise... downgrading to an older version of react router would be a simpler solution.

glass pawn
sonic sequoia
turbid bridge
sonic sequoia
turbid bridge
sonic sequoia
#

True, I was more correlating "problem" with "performance" more than anything. I guess "ideal" would be a more appropriate word to use 😅