Next.js >= 13 with AppDir enabled
You can use next/navigation
to redirect both in client components and server components.
Ex. in pages :
import { redirect } from 'next/navigation';
export default async function Home({ params }) {
redirect('/hello-nextjs');
// ...
}
Ex. In client components:
'use client';
import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
export const Home= () => {
const { push } = useRouter();
useEffect(() => {
push('/hello-nextjs');
}, []);
return <p></p>;
};