useRouter

useRouter

Overview

The useRouter hook provides access to various data about the current route, as well as methods to navigate between pages, as seen in the below example:

import { JSX } from 'react'
import { useRouter } from 'tuono'

export default function IndexPage(): JSX.Element {
  const router = useRouter()

  return (
    <>
      <p>pathname: {router.pathname}</p>
      <button
        onClick={() => {
          router.push('/foo')
        }}
      >
        My link
      </button>
    </>
  )
}

useRouter result

  • pathname: string - Returns the current path name
  • query: Record<string, string> - This object contains all the query params of the current route

The following methods are included inside router:

router.push

Handles client side page navigation quickly. Useful for internal links, or for when you need to change url in some JS.

router.push(path, options)

router.replace

Handles client side page navigation in the same way as router.push, but it will replace the current entry in the browser history stack.

router.replace(path, options)

Router methods parameters

NameTypeDefault valueDescription
pathstring (Required)-The path of the page you want to navigate to.
optionsNavigationOptions-Optional config object for additional control.
options.scrollbooleantrueIf false the scroll offset will be kept across page navigation.
Edit page