Skip to content

Instantly share code, notes, and snippets.

@Bwogi
Last active November 21, 2021 03:28
Show Gist options
  • Select an option

  • Save Bwogi/e81a97eea6757f84b8a71dd553acf9c3 to your computer and use it in GitHub Desktop.

Select an option

Save Bwogi/e81a97eea6757f84b8a71dd553acf9c3 to your computer and use it in GitHub Desktop.
This is how you link an article to itself(main) by id in next.js

Linking an article to the main article by id

Take special interest in the following code

<Link href='/article/[id]' as={`/article/${article.id}`}>

This is the main article

I have called this ArticleItem.js

import articleStyles from '../styles/Article.module.css'
import Link from 'next/link'
import { Fragment } from 'react'

const ArticleItem = ({ article }) => {
  return (
    <Fragment>
      <Link href='/article/[id]' as={`/article/${article.id}`}>
        <a className={articleStyles.card}>
          <h3>{article.title} &rarr</h3>
          <p>{article.body}</p>
        </a>
      </Link>
    </Fragment>
  )
}

export default ArticleItem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment