# How to programmatically change a route in Next.js

> Learn how to programmatically change a route in Next.js with the useRouter hook and router.push(), or the Router object when you are outside a component.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2021-05-10 | Topics: [Next.js](https://flaviocopes.com/tags/next/) | Canonical: https://flaviocopes.com/nextjs-programmatically-change-route/

In a component, you can use the `useRouter` hook:

```js
import { useRouter } from 'next/router'

//...

const router = useRouter()

router.push('/test')
```

Sometimes you can't, for example when you're not in a [React](https://flaviocopes.com/react/) component, maybe in a utility function.

In that case, you can do this:

```js
import Router from 'next/router'

Router.push('/test')
```
