Why this redirect doesnt work
Answered
JChicano posted this in #help-forum
JChicanoOP
Note: The "logOut" function is working properly and the code is entering inside the first if
21 Replies
Your inside a client component
you need to user the router.
JChicanoOP
But i used it in another part of my app inside a client component and it works
and the router give me an error
@Jboncz Click to see attachment
Its not possible. lol redirect() is just returning a 307 to the client to redirect.
@JChicano and the router give me an error
JChicanoOP
if i change it
Please send me the code.
not a picture, put the code here
JChicanoOP
"use client";
import LogOut from './cerrarSesionCookie';
import { useRouter } from 'next/router';
export default function CerrarSesion() {
const router = useRouter();
const handleBoton = async () => {
const response = await LogOut();
if (response) {
router.push('/');
} else {
console.log('Ha sucedido un error');
}
};
return (
<button className="bg-red-300 p-2 text-black cursor-pointer" onClick={handleBoton}>
Cerrar sesion
</button>
);
}
import LogOut from './cerrarSesionCookie';
import { useRouter } from 'next/router';
export default function CerrarSesion() {
const router = useRouter();
const handleBoton = async () => {
const response = await LogOut();
if (response) {
router.push('/');
} else {
console.log('Ha sucedido un error');
}
};
return (
<button className="bg-red-300 p-2 text-black cursor-pointer" onClick={handleBoton}>
Cerrar sesion
</button>
);
}
One sec
JChicanoOP
ye, np
import { useRouter } from "next/navigation";
Answer
Ugh, homie stealing my thunder
JChicanoOP
it works 

Of course it does 🙂
JChicanoOP
ty guys hahahhaha
Np
JChicanoOP
ye, the error is always the easier thing
like I said earlier, redirect is ONLY for server components, server actions and routehandlers