next.jsquery-string

How can I get (query string) parameters from the URL in Next.js?


When I click on a link in my /index.js, it brings me to /about.js page.

However, when I'm passing a parameter name through the URL (like /about?name=leangchhean) from /index.js to /about.js, I don't know how to get it in the /about.js page.

index.js

import Link from 'next/link';
export default () => (
  <div>
    Click{' '}
    <Link href={{ pathname: 'about', query: { name: 'leangchhean' } }}>
      <a>here</a>
    </Link>{' '}
    to read more
  </div>
);

Solution

  • Get it by using the below code in the about.js page:

    // pages/about.js
    import Link from 'next/link'
    export default ({ url: { query: { name } } }) => (
      <p>Welcome to About! { name }</p>
    )