Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NextJS URL params like React-Router

I'm a newbie to NextJS, It looks so good on the first impression. But after giving it a chance I've faced some problems like URL routing with custom params like react-router.

Currently what We can do with NextJS

https://url.com/users?id:123

What We need to have for better URL pattern

https://url.com/users/123

In react-router It has perfect example here https://reacttraining.com/react-router/web/example/url-params

like image 958
Nishchit Avatar asked Apr 03 '17 09:04

Nishchit


People also ask

Can I use React router in NextJS?

In React JS, we would install a package called react-router-dom to implement routing inside the application. But Next JS has its own inbuilt router from the next/link , with which we can navigate between the pages. Before using the next/link , we need to set up the different pages/routes inside the pages folder.

Does NextJS use client-side routing?

The Next. js router allows you to do client-side route transitions between pages, similar to a single-page application. A React component called Link is provided to do this client-side route transition.

How do I get NextJS Param?

To get the query parameter from the above URL inside the <Items> component, we can use the useRouter() hook in next. js.


1 Answers

For anyone arriving late to this party, we now have dynamic routing in Next 9.

Which would allow for a url structure to be crafted like this by using the file structure, and without additional packages.

You could create a file pages/user/[id].js

With

import { useRouter } from 'next/router'

const User = () => {
  const router = useRouter()
  const { id } = router.query

  return <p>User: {id}</p>
}

export default User
like image 132
Davey Avatar answered Oct 22 '22 20:10

Davey