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

后端 未结 8 551
情深已故
情深已故 2020-12-13 01:32

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

8条回答
  •  轮回少年
    2020-12-13 02:16

    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}) => (
      
    Click here to read more
    ) 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 
    Click here to read more
    } }

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

提交回复
热议问题