0

Is there is a tool or quick way to convert arbitrary HTML strings to valid JSX strings? And by string, I mean an actual string.

const htmlString = "<button class="foo" onclick="..." >Hello World</button>"
const jsxString = htmlStringToJsxString(htmlString)
console.log(jsxString)

And my desired output:

"<button className="foo" onClick="..." >Hello World</button>"

I'm not trying to parse HTML then render JSX, nor am I injecting HTML into the DOM. I specifically need to convert an HTML string to the conventions of JSX (camel-casing attributes, replacing "class" with "className", etc).

mkrkelj1
  • 61
  • 4
  • You'd get a lot better help if you answered: why? – Adam Dec 19 '21 at 23:56
  • ....but I suppose there's always: https://www.npmjs.com/package/node-html-parser – Adam Dec 19 '21 at 23:57
  • How deep does the translation need to go? Do you need the capability to transform ANY html string to JSX, or is there a limited use case here? – Andrew Dec 20 '21 at 00:44

0 Answers0