šŸŽ Checkout my Learn React by Making a Game course and get 1 month Free on Skillshare!

First routes in NextJs – static pages and router query params

Let's see how we can set up the routes in our NextJs apps. By the end of this tutorial, we will build a simple app with two static pages and one page that reads an id query param and prints its value on the page. Below you can see the final screenshots of the index page:

And the page containing a query param:

We will start creating the NextJs app from an empty folder.

We will then create a pages/index.js file with the following code:

export default () => (
    <div>
        <h1>Index page!</h1>
    </div>
)

Now if we run npm run dev we will see the above page at http://localhost:3000/.

NextJs maps the basic static routes to the folder structure of the pages folder. We will create a new pages/catalog/index.js page:

export default () => (
    <div>
        <h1>Main product catalog page!</h1>
    </div>
)

And now if we navigate to http://localhost:3000/catalog we will see the "Main product catalog page".

If we want to create a link from the main index page to the catalog page we will need to import and use the Link compoent from next/link.

import Link from 'next/link'
export default () => (
    <div>
        <h1>Index page!</h1>
        <ul>
            <li><Link href='/catalog'>Goto to main product catalog page</Link></li>
        </ul>
    </div>
)

Do not use the standard <a href="/catalog"> as you will lose the huge speed improvement given by the NextJs prefetching (and also you may get some wired bugs).

But what if we want to have some dynamic data in our NextJs routes? For example, if we want to have a variable id of a product with url's like this:

http://localhost:3000/catalog/1
http://localhost:3000/catalog/2
.....
http://localhost:3000/catalog/123

For this, we can use the Dynamic Routes of NextJs. We will first need to create the js file that will be mapped to that page format. Given that part of the url will be a query param we will need to delimitate that part with square brackets. So we will add a new pages/catalog/[id].js file containing the following code:

import { useRouter } from 'next/router'
export default () => {
    const router = useRouter()
    const {id} = router.query
    return <p>Single product with id: <strong style={{color: 'red'}}>{id}</strong></p>
}

Now, whenever we will have a Link similar to <Link href='/catalog/3'>Goto to product 3</Link> you will a page like the one below:

The useRouter hook will give us access to the query params. Be sure to have the same value in the square brackets as you have in router.query. So, for example, if you have pages/catalog/[title].js you will also need to change to const {title} = router.query

You can see the full code here and the deployed app is on GitHub Pages here.

šŸ“– 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.

šŸ“– 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.


Leave a Reply

Your email address will not be published. Required fields are marked *