skip to Main Content

I have a file like blog-details/[id].tsx. And I am trying to navigate via:

<Link href={`/blog-details/${item._id}.tsx`}>` 

After the page navigates, I need to fetch the data from db using the id, but the issue here is I am unable to access context.params from getServerSideProps.

const getServerSideProps = async (context) => {
  const params = context.params;
  console.log('route params ', params); // This doesn't get logged on page route
}

2

Answers


  1. You can try

    const id = context.query.id;
    
    Login or Signup to reply.
  2. First, make sure you are exporting getServerSidePros from /blog-details/[id].tsx file:

    export const getServerSideProps = async (context) => {
      const params = context.params;
      console.log('route params ', params);
      return { props: {} };
    }
    

    Then you don’t need to add this .tsx at the end of the URL:

    <Link href={/blog-details/${item._id}>Go To Blog Details</Link>
    

    Unless you want to get a someId.tsx.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search