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

NextJS render content from a different page

I’ve two routes like,

app/products => pages/products/index.js
app/products/1 => pages/products/[page].js

Here both app/products and app/product/1 will render the same content (same product items), is it possible to render app/products/1 content in app/products without writing duplicate code? I could find anything similar in their documentation.

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

Thanks.

>Solution :

The easiest way is to abstract the content from the page level. No need for tricky code here.
pages/products/index.js:

import { ProductPageContent } from 'components/ProductPageContent';
import { getProductData } from 'lib/productData';

export default function ProductsIndexPage() {
  return <ProductPageContent />
}

export async function getStaticProps() {
  const productData = await getProductData();
  return {
     productData,
  }
}

pages/products/[page].js:

import { ProductPageContent } from 'components/ProductPageContent';
import { getProductData } from 'lib/productData';

export default function ProductPagePage() {
 return <ProductPageContent />
}

export async function getStaticProps() {
  const productData = await getProductData();
  return {
     productData,
  }
}
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