Search code examples
reactjsnext.jsnext-router

I want to redirect a user in Next without using <Link>. React Router has useNavigate(), so does Next Router have anything similar?


I want to redirect users in NextJS similar to how you can use useNavigate(). <Link /> is not good enough for this, because I want the user to be immediately redirected.


Solution

  • I suppose you would be looking for the useRouter hook to access the router object and issue an imperative navigation action.

    Example:

    const router = useRouter();
    
    ...
    
    router.replace(targetInAppUrl);
    

    See router.push or router.replace for more details.