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

142
Visualizações
How to integrate compound React(Typescript) component into html page

I have the old website which was built by Vanilia Javascript. And now I am converting it to React. So I am going to render compound React(Typescript) component in HTML page. But it does not work. Here is my code.

app.html

<body>
  <div id="test-id">
  </div>
  <script src="https://unpkg.com/react@15/dist/react.min.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js" crossorigin></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script>
  <script type="text/tsx" src="../test/component/Test.tsx"></script>
</body>

My component - Test.tsx

import { makeStyles, ThemeProvider } from '@material-ui/core';
import { Web3Provider } from '@ethersproject/providers';
import { useWeb3React } from '@web3-react/core';
import { useEffect, useState } from 'react';
import Header from '../Layout/Header';
import { api } from '../../services/api/api';
import TestMain from './TestMain';
import Footer from '../Footer/Footer';

const useStyles = makeStyles((theme) => ({
 ... some styles
}));

type Props = {};

const Test: React.FC<any> = () => {
  const classes = useStyles();

  let data: [] = [...example data]

  return (
    <div className={classes.root}>
      <Header login={true} color="primary"/>
      <main className={classes.main}>
        <TestMain data={data} />
      </main>
      <Footer />
    </div>
  );
}

ReactDOM.render(<Test />, document.getElementById("test-id"))

I found many examples for this, but it was a simple component. It worked for me. But when I try to render my component in HTML, it does not work. My component includes external component, Material UI and etc. Is it possible to render my component in HTML page? If yes, please anyone help me.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is probably not easily possible.

(Note: actually I'm thinking of a webpack setup right now. I never used parcel, as recommended in the comments to your question, and I don't know the possibilities there)

HTML and the React app source code are very different technologies, basically:

  • React is Javascript / JSX / Typescript / ... code that is compiled and generates HTML pages, and
  • a HTML website is HTML pages, (probably) including some javascript code

React code just doesn't know anything about the static HTML files, and the HTML files can not compile the React code.

The React app includes e.g. these technologies:

  • modules (see also How to isolate components in React app.js bundle)
  • Typescript, needs to be "compiled out"
  • JSX, needs to be compiled to javascript

There are solutions (more or less) for all these individual technologies, but in combination it becomes quite complicated, and it's probably less work to refactor the whole HTML website to React code (or more efficient to do it all at once, instead of bit by bit).

There are some different approaches:

  • Build a React app and copy the old HTML code into it
  • HTML pages, ReactDOM.render() in one or more places
  • React code using dangerouslySetInnerHTML
  • Setup some complex tooling and workflow that is combining the old HTML code and the HTML code that is generated by React, after the React compiling
  • ...

See also:

  • How to embed React Components in html pages
  • How to use React components as part of legacy HTML/JS app
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