Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
How to render pages dynamically in nextjs

I am rendering a webpage in frontend, taking the data for webpage from the database. Right now I have hard coded the route for a perticular webpage, but I want to make it dynamic as there are multiple webpages in the db and also dynamically render them on nextjs with unique url.

Following is the code for rendering the webpage

import React, { useEffect, useState } from "react";
import axios from "axios";
import NavigationBar from "../../components/Navbar"
import Footer from "../../components/Footer";
// import { useParams } from "react-router";

export default function Webpage() {
  // let { slug }: any = useParams();
  const [webpage, setWebpage] = useState("");
  // console.log(slug, "slug")
  useEffect(() => {
    async function renderPage() {
      const response = await axios.get(
        "http://localhost:8080/dfsk-ec35-docs"
      );
      setWebpage(response.data);
    }
    renderPage();
  }, []);
  
  return (
    <div>
      {/* Navigation bar component */}
      <NavigationBar type="main"/>
      {/* rendering the web page */}
      <div dangerouslySetInnerHTML={{ __html: webpage}} />
      {/* Footer component */}
      <Footer />
    </div>
  );
}

Following is the code I have for dynamic routes.

import { useRouter } from "next/dist/client/router";
import Webpage from "./webpage"
export default function WebpageId(){
 const router = useRouter()
 const  pid  = router.query.id
    return(
        <div>page: {pid}</div>
        // <Webpage/>
    )
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can pass in the pid you extracted as a prop into the Webpage component. You can also use useRouter() to get the id in your Webpage component directly. Example:

import React, { useEffect, useState } from "react";
import axios from "axios";
import NavigationBar from "../../components/Navbar"
import Footer from "../../components/Footer";
// import { useParams } from "react-router";

export default function Webpage() {
  // let { slug }: any = useParams();
 const router = useRouter()
 const  pid  = router.query.id
  const [webpage, setWebpage] = useState("");
  // console.log(slug, "slug")
  useEffect(() => {
    async function renderPage() {
      const response = await axios.get(
        `http://localhost:8080/${pid}`
      );
      setWebpage(response.data);
    }
    renderPage();
  }, []);
  
  return (
    <div>
      {/* Navigation bar component */}
      <NavigationBar type="main"/>
      {/* rendering the web page */}
      <div dangerouslySetInnerHTML={{ __html: webpage}} />
      {/* Footer component */}
      <Footer />
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!