Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 parameter name through 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>
);
like image 609
Sour LeangChhean Avatar asked May 09 '17 06:05

Sour LeangChhean


People also ask

How can I get parameters from a URL string?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.

How do you access parameters in Nextjs?

You can access it inside the <Items> component like this. import React, { Component } from "react"; import { withRouter } from "next/router" class Items extends Component { render() { const { query } = this.

What is used to extract the query parameters from the URL?

QueryParam annotation in the method parameter arguments. The following example (from the sparklines sample application) demonstrates using @QueryParam to extract query parameters from the Query component of the request URL.

Can you use Javascript to get URL parameter values?

The short answer is yes Javascript can parse URL parameter values. You can do this by leveraging URL Parameters to: Pass values from one page to another using the Javascript Get Method. Pass custom values to Google Analytics using the Google Tag Manager URL Variable which works the same as using a Javascript function.


3 Answers

Use router-hook.

You can use the useRouter hook in any component in your application.

https://nextjs.org/docs/api-reference/next/router#userouter

pass Param

import Link from "next/link";  <Link href={{ pathname: '/search', query: { keyword: 'this way' } }}><a>path</a></Link> 
Or
import Router from 'next/router'  Router.push({     pathname: '/search',     query: { keyword: 'this way' }, }) 

In Component

import { useRouter } from 'next/router'  export default () => {   const router = useRouter()   console.log(router.query);    ... } 
like image 193
junho Avatar answered Oct 03 '22 11:10

junho


Using Next.js 9 or above you can get query parameters:

With router:

import { useRouter } from 'next/router'  const Index = () => {   const router = useRouter()   const {id} = router.query    return(<div>{id}</div>) } 

With getInitialProps:

const Index = ({id}) => {   return(<div>{id}</div>) }  Index.getInitialProps = async ({ query }) => {   const {id} = query    return {id} } 
like image 35
Noodles Avatar answered Oct 03 '22 11:10

Noodles


url prop is deprecated as of Next.js version 6: https://github.com/zeit/next.js/blob/master/errors/url-deprecated.md

To get the query parameters, use getInitialProps:

For stateless components

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

About.getInitialProps = ({query}) => {
  return {query}
}

export default About;

For regular components

class About extends React.Component {

  static getInitialProps({query}) {
    return {query}
  }

  render() {
    console.log(this.props.query) // The query is available in the props object
    return <div>Click <Link href={{ pathname: 'about', query: { name: 'leangchhean' }}}><a>here</a></Link> to read more</div>

  }
}

The query object will be like: url.com?a=1&b=2&c=3 becomes: {a:1, b:2, c:3}

like image 32
Brunno Vodola Martins Avatar answered Oct 03 '22 09:10

Brunno Vodola Martins