Next.js Discord

Discord Forum

Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted

Answered
Schneider’s Smooth-fronted Caima… posted this in #help-forum
Open in Discord
Schneider’s Smooth-fronted CaimanOP
import { useState } from "react";
import toast from "react-hot-toast";
import { useAuthContext } from "../context/AuthContext";
import { useRouter } from "next/router";

const useLogout = () => {
  const router = useRouter();
  const [loading, setLoading] = useState(false);
  // @ts-ignore
  const { setAuthUser } = useAuthContext();
  const logout = async () => {
    try {
      setLoading(true);
      const res = await fetch("/api/auth/logout");

      const data = await res.json();

      if (data.error) {
        throw new Error(data.error);
      }

      localStorage.removeItem("Auth");

      setAuthUser(null);

      toast.success("Logged Out");
      router.push("/login");
    } catch (err: any) {
      toast.error(err.message);
      console.log(err);
    } finally {
      setLoading(false);
    }
  };
  return { loading, logout };
};

export default useLogout;

why do i get this error im using "use client" in the component
Answered by ItetsuLaTable
If you are using app dir you must use next/navigation not next/router
View full answer

6 Replies

Hello @Schneider’s Smooth-fronted Caiman ! Are you using app or pages dir ?
If you are using app dir you must use next/navigation not next/router
Answer
Schneider’s Smooth-fronted CaimanOP
fixed it thank u so much
Welcome 🙂
Next change the router between pages and app dir.

next/navigation works well in pages dir but next/router isnt supported in app dir 😦
Schneider’s Smooth-fronted CaimanOP
its cool works out for me