Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda