Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Important
Security Advisory: React2Shell & two new vulnerabilities
Find out more

useRouter

Last updated October 22, 2025

TheuseRouter hook allows you to programmatically change routes insideClient Components.

Recommendation: Use the<Link> component for navigation unless you have a specific requirement for usinguseRouter.

app/example-client-component.tsx
'use client'import { useRouter }from'next/navigation'exportdefaultfunctionPage() {constrouter=useRouter()return (    <buttontype="button"onClick={()=>router.push('/dashboard')}>      Dashboard    </button>  )}

useRouter()

  • router.push(href: string, { scroll: boolean }): Perform a client-side navigation to the provided route. Adds a new entry into thebrowser's history stack.
  • router.replace(href: string, { scroll: boolean }): Perform a client-side navigation to the provided route without adding a new entry into the browser’s history stack.
  • router.refresh(): Refresh the current route. Making a new request to the server, re-fetching data requests, and re-rendering Server Components. The client will merge the updated React Server Component payload without losing unaffected client-side React (e.g.useState) or browser state (e.g. scroll position).
  • router.prefetch(href: string, options?: { onInvalidate?: () => void }):Prefetch the provided route for faster client-side transitions. The optionalonInvalidate callback is called when theprefetched data becomes stale.
  • router.back(): Navigate back to the previous route in the browser’s history stack.
  • router.forward(): Navigate forwards to the next page in the browser’s history stack.

Good to know:

  • You must not send untrusted or unsanitized URLs torouter.push orrouter.replace, as this can open your site to cross-site scripting (XSS) vulnerabilities. For example,#"">Migrating fromnext/router
    • TheuseRouter hook should be imported fromnext/navigation and notnext/router when using the App Router
    • Thepathname string has been removed and is replaced byusePathname()
    • Thequery object has been removed and is replaced byuseSearchParams()
    • router.events has been replaced.See below.

    View the full migration guide.

    Examples

    Router events

    You can listen for page changes by composing other Client Component hooks likeusePathname anduseSearchParams.

    app/components/navigation-events.js
    'use client'import { useEffect }from'react'import { usePathname, useSearchParams }from'next/navigation'exportfunctionNavigationEvents() {constpathname=usePathname()constsearchParams=useSearchParams()useEffect(()=> {consturl=`${pathname}?${searchParams}`console.log(url)// You can now use the current URL// ...  }, [pathname, searchParams])return'...'}

    Which can be imported into a layout.

    app/layout.js
    import { Suspense }from'react'import { NavigationEvents }from'./components/navigation-events'exportdefaultfunctionLayout({ children }) {return (    <htmllang="en">      <body>        {children}        <Suspensefallback={null}>          <NavigationEvents />        </Suspense>      </body>    </html>  )}

    Good to know:<NavigationEvents> is wrapped in aSuspense boundary becauseuseSearchParams() causes client-side rendering up to the closestSuspense boundary duringstatic rendering.Learn more.

    Disabling scroll to top

    By default, Next.js will scroll to the top of the page when navigating to a new route. You can disable this behavior by passingscroll: false torouter.push() orrouter.replace().

    app/example-client-component.tsx
    'use client'import { useRouter }from'next/navigation'exportdefaultfunctionPage() {constrouter=useRouter()return (    <buttontype="button"onClick={()=>router.push('/dashboard', { scroll:false })}    >      Dashboard    </button>  )}

    Version History

    VersionChanges
    v15.4.0OptionalonInvalidate callback forrouter.prefetch introduced
    v13.0.0useRouter fromnext/navigation introduced.

Was this helpful?

supported.

[8]ページ先頭

©2009-2025 Movatter.jp