This tutorial will walk you through the steps to add a simple back button to your Next.js project, offering a more app-like experience by allowing users to navigate to the previous page with just a click.
If you have an existing project then you can skip the setup steps but if you're starting from scratch you can create a new project in two ways, either buy manually creating the files or using npx. A full setup guide can be found in the Next.js docs if you'd prefer to read it in full.
This will create a basic project using the Next.js cli with npx - meaning you won't need to install next-cli globally on your computer.
npx create-next-app
# or
yarn create next-app
You can also create a project from scratch, which is what I personally prefer to do, and add everything you need manually. This can be done in three simple steps.
npm init
to create a new package.json file, you can name the project whatever you like and go through each step adding whatever info you need. Once created, open package.json and add the following scripts:"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
A short explanation of what each script does:
dev - runs next dev which starts Next.js in development mode
build - runs next build which builds the application for production usage
start - runs next start which starts a Next.js production server
npm install next react react-dom
# or
yarn add next react react-dom
function HomePage() {
return (
<div>
<h1>Homepage</h1>
</div>
)
}
export default HomePage
Adding a back button to you project is really easy with Next.js, by default, Next comes with access to a router. You can import it and use it from the next package using the following syntax:
import { useRouter } from "next/router"
const router = useRouter()
This gives you access to the router object, including information such as:
You can also access router methods such as router.replace()
, router.reload()
or router.back()
, which is what we'll need for the back button.
You can add the back button anywhere in your project, but for this tutorial we'll create a header component and add the button there, this will allow you to re-use the button on each page of your app by importing the header. You can create a 'components' folder in the root of your project and add a Header.js file there.
import { useRouter } from "next/router"
const Header = () => {
const router = useRouter()
return (
<header>
<button onClick={() => router.back()}>Back</button>
<nav>
<a href="/">Home</a>
</nav>
</header>
)
}
export default Header
This is just a simple example of a header with navigation tags and a link to the homepage, you can add to this and style it however you like but for this tutorial a simple header component will be enough.
You could also go further and wrap the button in a conditional statement that only displays the back button on pages other than the homepage.
<header>
{router.pathname !== "/" && (
<button onClick={() => router.back()}>Back</button>
)}
<nav>
<a href="/">Home</a>
</nav>
</header>
The above code compares the current URL path (extracted from the router object) with the homepage path '/' and only shows the button if they are not equal.
Open the home page component and import the newly created 'Header.js' componenet with your back button.
import Header from "../components/Header"
function HomePage() {
return (
<div>
<Header />
<h1>Homepage</h1>
</div>
)
}
export default HomePage
You now have access to the back button on every page of your app. This is a simple example of how easy it is to add functionality such as a back button to your project using the Next.js API features.