Can't use router in Next 14
Answered
Toy Manchester Terrier posted this in #help-forum
Toy Manchester TerrierOP
I'm trying to use "useRouter()" in a page.js with "use client"
to change localization.
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>
);
}
Answered by josh
Passing
instead
locale
to router.replace/push
was removed. You can just dorouter.replace(`/${locale}${pathname}`)
instead
4 Replies
Passing
instead
locale
to router.replace/push
was removed. You can just dorouter.replace(`/${locale}${pathname}`)
instead
Answer
Toy Manchester TerrierOP
Ty
it worked? 🙂 you can mark the message as a solution with Right click > Apps > Mark Solution