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

147
Visualizações
parse mixed jsx and html in react

I would like to parse html generated with tinymce, modified by adding jsx, in react.

Example:

import Youtube from "react-youtube"
function Comment() {
  return (
    <div class="comment">
     // I want something that can parse like this
     {parse(`<p>
          <code class="language-javascript">console.log("hi")</code>
          <Youtube videoId="10"/>
          <a href="https://youtu.be/stackoverflow">Youtube</a>
         </p>`)}
    </div>
  )
}

I have come across react-jsx-parser, which doesn't work as expected :(

I have also tried converting html string to jsx string:

import parse from "html-react-parser";
import ReactElementToJsxString from "react-element-to-jsx-string";
import { parse as parsehtml } from "node-html-parser";
function modifycomment () {
  const comment = ReactElementToJsxString(parse(`<p>
          <code class="language-javascript">console.log("hi")</code>
          <a href="https://youtu.be/stackoverflow">Youtube</a>
         </p>`));
 const link = parsehtml(comment).querySelector("a");
 return comment.replace(link.toString(), 
 `<Youtube videoId="${link.getAttribute("href"),replace("https://youtu.be/", "")}"${link.toString()}`);;
}

Then parse it:

import Youtube from "react-youtube";
import JsxParser from "react-jsx-parser";
function Comment () {
   return (
     <div class="comment">
       <JsxParser
        components={{ Youtube }}
        jsx={modifycomment()}
       />
     </div>
   )
}

which led to errors in rendering (something like the child to remove is not a child of this Node) :(

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

0

Based on react-youtube documentation you just need to pass the video Id. But your code has an extra <a> tag.

comment.replace(link.toString(), 
 `<Youtube videoId="${link.getAttribute("href"),replace("https://youtu.be/", "")}"${link.toString()}`);

and you are not assigning this to anything. I have made changes and did a sample one. Check this sandbox

By the way, I took some sample video

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