0

First routes in NextJs – static pages and router query params

 1 year ago
source link: http://www.js-craft.io/blog/first-routes-in-nextjs-static-pages-and-router-query-params/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:

Screen-Shot-2022-09-21-at-10.07.16.png

And the page containing a query param:

Screen-Shot-2022-09-21-at-10.07.24.png

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:

Screen-Shot-2022-09-21-at-10.55.19.png

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.

I hope you have enjoyed this article and if you would like to get more articles about React and frontend development you can always sign up for my email list.

Newsletter subscribe:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK