Next.js Discord

Discord Forum

Why this redirect doesnt work

Answered
JChicano posted this in #help-forum
Open in Discord
Note: The "logOut" function is working properly and the code is entering inside the first if
Answered by Jboncz
import { useRouter } from "next/navigation";
View full answer

21 Replies

Your inside a client component
you need to user the router.
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
if i change it
Please send me the code.
not a picture, put the code here
"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>
);
}
One sec
ye, np
import { useRouter } from "next/navigation";
Answer
Ugh, homie stealing my thunder
it works :lolsob:
Of course it does 🙂
ty guys hahahhaha
Np
ye, the error is always the easier thing
like I said earlier, redirect is ONLY for server components, server actions and routehandlers