How to access data passed from a component to a page via <LInk> in nextjs

I have a nextjs app . I have used this answer to pass data from a component to a page using the Link like this.

                <Link href={{
                  pathname: "/products/"+id,
                  query: userId // the data
                  <a>Show more</a>

So I am able to see the extra data that I have passed on the receiving page as an object like this,

const router = useRouter();
const data  = router.query;

  console.log("USER ID IS!!!!!!", data)

but the console log gives me the value with an empty key like this

id: "12"}

It also gets appended on the url like this


How can I access the 990 ? eg I can get the id like this . But what about the userId ?

>Solution :

Your current url is products/12?990. It should ideally be products/12?userId=990. The reason for that is that you are passing a primitive string like query: userId, which translates to query: 990.

You are supposed to pass an object according to the docs like :

 query: { userId : userId } 

or simply:

 query: { userId } 

Leave a Reply