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

522
Visualizações
How to convert HTMLElement to JSX.Element

Here I have something like below as HTMLElement

<div id='hiddenDiv'>
  <div>foobar</div>
</div>

And I am doing something like below trying to convert it into JSX.Element:

function renderMailCompose(): JSX.Element {
    const content = document.getElementById('hiddenDiv');
    return content;
}

but I am getting the error

Type 'HTMLElement' is missing the following properties from type 'ReactElement<any, any>': type, props, key

I am wondering how I can properly convert the div including all its content to JSX.Element. Any help would be appreciated!

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

0

Generally, unless you have a specific reason for using getElementById, what we usually want to do in-order to store elements inside variables in React, is to use the built-in useRef hook.

So you can do this -

const hiddenDivRef = useRef<HTMLDivElement>(null);

return (
  <div ref={hiddenDivRef}>
    <div>foobar</div>
  </div>
);

After which we can access the value like this -

console.log(hiddenDivRef.current);

You can also use all the relevant functions you would normally have,
i.e. - .focus(), etc...

  • Note: The hook is not exclusive to storing elements.
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use html-react-parser to render html like this:

npm install html-react-parser --save
import parse from 'html-react-parser';

function renderMailCompose(): JSX.Element {
    const content = document.getElementById('hiddenDiv');
    return parse(content.innerHTML);
}
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