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.
import Link from 'next/link';
export default () => (
<div>
Click{' '}
<Link href={{ pathname: 'about', query: { name: 'leangchhean' } }}>
<a>here</a>
</Link>{' '}
to read more
</div>
);
Use router-hook.
You can use the useRouter hook
in any component in your application.
https://nextjs.org/docs/api-reference/next/router#userouter
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' },
})
import { useRouter } from 'next/router'
export default () => {
const router = useRouter()
console.log(router.query);
...
}