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

后端 未结 8 561
情深已故
情深已故 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:28

    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";
    
    path
    
    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);
    
      ...
    }
    

提交回复
热议问题