Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!