useRouter (app dir)
Use `useRouter` hook
Breaking Change
As of version 3.2.0
, router push
and replace
methods now have only 2 parameters (href
and options
).
You can modify your router calls as follows:
The app directory router doesn't manage routes like the page directory router. So we need a custom router that extends the Next.js router and enables the progress bar.
Warning
The useRouter
hook can only be used in the app directory. It will not work
in the pages directory.
Import
Usage
See the Next.js useRouter
hook documentation for more information.
Options
Name | Type | Default | Description |
---|---|---|---|
showProgress | boolean | true | Show the progress bar. |
startPosition | number | undefined | The start position of the progress bar. |
disableSameURL | boolean | true | Disable the progress bar when the URL is the same. |
basePath | string | undefined | The base path if you use a basePath in your app. |
i18nPath | boolean | false | Takes into account the first parameter of the URL (e.g. /en) |
delay | number | undefined | The delay before the progress bar starts. |
stopDelay | number | undefined | The delay before the progress bar stops. |
customRouter | () => AppRouterInstance | undefined | A custom router instance. |
Note
By default, if you do not specify any options when calling a router method,
the options defined during the initialization of the useRouter
hook will be
applied. However, if you provide specific options in the method call (push
,
replace
, back
, refresh
), these will take precedence and override the
default options set in the hook.
RouterProgressOptions
AppRouterProgressInstance
CombinedRouterOptions
Methods
push
Usage
Pushes a new entry onto the history stack.
Types
replace
Usage
Replaces the current entry on the history stack.
Types
back
Usage
Goes back in the history stack.
Types
forward
Usage
Goes forward in the history stack.
Types
refresh
Usage
Refreshes the current page.
Types
Options
Usage
Types
Name | Type | Default | Description |
---|---|---|---|
showProgress | boolean | true | Show the progress bar. |
startPosition | number | undefined | The start position of the progress bar. |
disableSameURL | boolean | true | Disable the progress bar when the URL is the same. |
basePath | string | undefined | The base path if you use a basePath in your app. |
i18nPath | boolean | false | Takes into account the first parameter of the URL (e.g. /en) |
delay | number | undefined | The delay before the progress bar starts. |
stopDelay | number | undefined | The delay before the progress bar stops. |
...customRouterOptions | any | undefined | Any custom options for the router, by default we use the next NavigateOptions . |
Note
The showProgress
option set to false
disables the progress bar. However,
if you don't want to see the progress bar during a router action, we recommend
you use the Next.js router directly.