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

75
Visualizações
replace react .tsx file template content with actual data

wondering if it's possible to somehow replace a template file with actual content (coming from an api). E.g. the template looks like this:

import { NextPage } from "next";

const Page: NextPage = () => {
    return (
        <Layout>
            {content}
        </Layout>
    )
};

export default Page;

the following would have to be replaced: Page with the actual file name, Layout with the corresponding layout that file uses, and content with the page`s content.

Now I am able to create a file already using the following:

import type { NextApiRequest, NextApiResponse } from "next";
import fs from "fs";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  const page_name: string = req.body.name;
  const page_slug: string = req.body.slug;
  const page_directory: string = req.body.directory;
  const page_layout: string = req.body.layout;
  const components: Array<string[]> = req.body.components;

  const page_data = {
    layout: page_layout,
    components,
  };

  fs.writeFile(
    `Pages/${page_name}`,
    JSON.stringify(page_data),
    function (error) {
      if (error) throw error;

      return res.json({
        message: "File has been created",
      });
    }
  );
}

e.g. if I where to call this api and pass in a name of about it would create a about.tsx file, but the content would obviously be empty.

How would I go about using a template file and replacing said content with content coming from the api?

about 4 years ago · Juan Pablo Isaza
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