#How to blocking navigation

1 messages · Page 1 of 1 (latest)

hollow groveBOT
#

Hi @bleak pulsar. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

In Next.js, you can control the navigation behavior using the Link component or the useRouter hook.

To disable the default scroll restoration behavior, you can pass scroll={false} to the Link component, or scroll: false to router.push() or router.replace(). Here are examples of how to do this:

Using Link:

// next/link
<Link href="/dashboard" scroll={false}>
 Dashboard
</Link>

Using useRouter:

// useRouter
import { useRouter } from 'next/navigation'
 
const router = useRouter()
 
router.push('/dashboard', { scroll: false })

Or in a button click event:

'use client'
 
import { useRouter } from 'next/navigation'
 
export default function Page() {
 const router = useRouter()
 
 return (
 <button
 type="button"
 onClick={() => router.push('/dashboard', { scroll: false })}
 >
 Dashboard
 </button>
 )
}

Please note that these methods only disable the scroll restoration behavior, not the navigation itself. If you want to block navigation completely, you would need to implement that logic in your application, as Next.js does not provide a built-in way to do this.

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@bleak pulsar kapa.ai is still learning and improving, please let us know how it did by reacting below