Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 https://stackoverflow.com/a/72221419/14287707 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>
                </Link>

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{990:""
id: "12"}

It also gets appended on the url like this

products/12?990

How can I access the 990 ? eg I can get the id like this data.id . 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 } 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading