Next.js Discord

Discord Forum

Can't use router in Next 14

Answered
Toy Manchester Terrier posted this in #help-forum
Open in Discord
Avatar
Toy Manchester TerrierOP
I'm trying to use "useRouter()" in a page.js with "use client"
to change localization.

"use client";

import Navbar from "../../components/landing-page/Navbar/Navbar";
import { useMessages } from 'next-intl';
import { usePathname, useRouter } from "next/navigation";
import { useState, useEffect } from "react";

export default function Home(props) {
  const pathname = usePathname();
  const router = useRouter();
  const [locale, setLocale] = useState(props.params.locale);

  useEffect(()=>{
    if (locale != props.params.locale) {
      console.log("hello");
      router.replace(pathname, {locale: locale}); // does nothing;
    }
  }, [locale])

  return (
    <main>
      <Navbar locale={locale} setLocale={setLocale}/>
    </main>
  );
}
Image
Answered by josh
Passing locale to router.replace/push was removed. You can just do

router.replace(`/${locale}${pathname}`)

instead
View full answer

4 Replies

Avatar
Passing locale to router.replace/push was removed. You can just do

router.replace(`/${locale}${pathname}`)

instead
Answer
Avatar
Toy Manchester TerrierOP
Ty
Avatar
it worked? 🙂 you can mark the message as a solution with Right click > Apps > Mark Solution