HTML to JSX Converter
WM-22Paste HTML to get JSX for React. It renames class and for, turns style strings into objects, self-closes void tags and converts comments.
Runs in your browser. Nothing is uploaded.
HTML
JSX
How to use it
- Paste your HTML or SVG into the input box.
- Leave Wrap several root nodes ticked if the markup has more than one top-level element.
- Choose Convert and read the JSX in the output box.
- Copy it into a component, then handle the items listed under what is not converted.
What is converted
class becomes className and for becomes htmlFor. A style attribute such as style="margin-top: 8px" becomes style={{ marginTop: '8px' }}. Void elements such as br, img and input are written as self-closing tags, and comments become {/* */}. Boolean attributes such as disabled="" become the bare name disabled. Listed attributes are renamed to camelCase, including tabindex, maxlength, colspan, SVG names such as stroke-width and fill-rule, and event names such as onclick. data- and aria- attributes stay as they are.
Text, pre blocks and open tags
Braces and the greater-than sign in text are escaped, because JSX does not allow them bare. Text in pre, even when it holds child tags such as code, becomes a string value so JSX does not trim its indentation. Where a line break separates text from an inline element such as a link, a {" "} is added so the space stays, and a textarea becomes a self-closing tag with defaultValue. Style and script bodies are wrapped in template strings. HTML lets you leave li, p, td and similar tags open, so those are closed for you. Any other unclosed element, or a closing tag that does not match, is reported with its line number.
What it does not convert
Event handler values stay as strings, so onClick="save()" must be rewritten as a function by you. checked and value are not changed to defaultChecked and defaultValue, so a React input may need that edit. Script tags are kept as text and React will not run them. Attributes that are not in the conversion list are left as typed, and a name JSX cannot accept, such as @click, is written as a spread. Components, state and logic are yours to add.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
Line 5: closing tag </div> has no matching opening tag. | A closing tag appears with no element open for it. | Remove it or add the opening tag. |
Line 8: </div> closes <span> from line 6, which is still open. | An element inside is missing its closing tag. | Close the inner element before the outer one. |
<section> opened at line 2 is never closed. | An element has no closing tag by the end of the input. | Add the closing tag. |
Line 3: the style declaration "color red" has no colon. | A style attribute holds a piece without a property and value. | Write it as property: value. |
Questions people ask
How do I convert HTML to JSX?
Paste the HTML and choose Convert. Attribute names, styles, comments and self-closing tags are rewritten. Then paste the result into the return statement of a React component.
How does the converter handle inline styles?
The CSS string is split on semicolons, outside quotes and parentheses, and each property becomes a camelCase key with a string value. Custom properties such as --gap keep their name as a quoted key. Values stay strings, which React accepts.
Can I convert an SVG?
Yes. SVG attributes with hyphens, such as stroke-width, fill-rule and clip-path, are camelCased, xlink:href becomes xlinkHref and a lowercase viewbox becomes viewBox. Check unusual attributes, since only a listed set is converted.
Why does my onclick still show a string?
JSX expects a function there, and the converter cannot know what your code should call. It renames the attribute to onClick and leaves the text, so you replace it with a handler such as onClick={save}.
Do I need to wrap the output in a fragment?
A React component must return one root node. If your HTML has several top-level elements, or text or a comment at the top, the tool wraps them in <> and </>. Untick the option to get the bare markup.
Will the output run as is?
Usually the markup is valid JSX, but React may still warn about things the tool leaves alone, such as a checked attribute without a handler. Treat it as a head start and run it through your build.
About this tool
Changes class to className, for to htmlFor, style strings to style objects, comments to {/* */}, and void elements such as br and img to self-closing form. It renames about 152 listed attributes, including event handlers (onclick to onClick), SVG names (stroke-width to strokeWidth) and names such as tabindex and maxlength; data- and aria- attributes stay as they are. It also escapes { } > in text, turns pre and textarea text into string values (text inside pre keeps its exact whitespace even when the pre has child tags), writes boolean attributes such as disabled="" as the bare name, adds {" "} where a line break separates text from an inline element so the space is kept, and closes li, p, td and similar elements that HTML lets you leave open. It does not turn event handler strings into functions (onClick="save()" stays a string and must be rewritten), does not convert checked or value to defaultChecked or defaultValue, does not make script tags run, and leaves unknown attributes as typed. Attributes JSX cannot name are written as a spread.