Skip to content

HTML to JSX Converter - Convert HTML to React

HTML input
JSX output

Turn HTML into valid JSX

Markup pasted into a React component from a template or an old page usually fails on the first compile: class instead of className, an unclosed <br>, a style string. Paste HTML on the left and get JSX on the right. Your browser parses the HTML, so unclosed tags are fixed up the same way a real page would fix them.

What gets converted

  • class becomes className and for becomes htmlFor.
  • Inline style strings become style objects with camelCase property names. CSS variables stay quoted.
  • Void and empty elements such as <br>, <img> and <input> are self-closed.
  • Attributes like tabindex, readonly, maxlength and autocomplete get their React names.
  • SVG attributes such as stroke-width and xlink:href become strokeWidth and xlinkHref.
  • Event attributes such as onclick become onClick with an arrow function.
  • HTML comments become JSX comments.
  • Boolean attributes such as disabled="" become plain disabled.
  • Several top-level elements are wrapped in a fragment, <>...</>.

How to use it

Paste HTML and the JSX appears as you type. Tick Wrap in a component to get an export default function you can save as its own file. The name must start with a capital letter, or React treats <loginForm /> as an HTML tag. Leave it unticked to get bare JSX for an existing component's return. Raw SVG icons convert the same way.

Example

Input:

<form class="login" onsubmit="return check()">
  <!-- Email field -->
  <label for="email">Email</label>
  <input type="email" id="email" tabindex="1" autofocus>
  <p style="color: #666; font-size: 12px">We never share your <b>email</b>.</p>
  <button class="btn btn-primary" disabled>Sign in</button>
</form>

Output (wrapped in a component called LoginForm):

export default function LoginForm() {
  return (
    <form className="login" onSubmit={(event) => { return check() }}>
      {/* Email field */}
      <label htmlFor="email">Email</label>
      <input type="email" id="email" tabIndex="1" autoFocus />
      <p style={{ color: "#666", fontSize: "12px" }}>We never share your <b>email</b>.</p>
      <button className="btn btn-primary" disabled>Sign in</button>
    </form>
  );
}

Whitespace is kept correct

JSX drops line breaks between text and tags, so Hello on one line and <b>world</b> on the next renders as Helloworld. To avoid that, any element that mixes text with inline tags is written on a single line, as the <p> above shows.

After converting

Replace inline event code with real handlers and decide which inputs need state. For a full page, paste the contents of <body> rather than the whole document. tabindex="1" comes out as the string tabIndex="1", which React accepts, though {1} is more usual. To tidy the source first, use the HTML formatter.

Related Tools

Frequently Asked Questions

Why can't I paste HTML straight into a React component?
JSX looks like HTML but compiles to JavaScript. Words such as class and for are reserved in JavaScript, so React uses className and htmlFor. Inline styles must be objects, every tag must be closed, and attribute names follow the DOM's camelCase naming.
What happens to inline event handlers like onclick?
They become camelCase props (onclick becomes onClick) with the code wrapped in an arrow function. Check each one by hand. Returning false from a React handler does not cancel the event the way onsubmit="return false" does, so you need event.preventDefault(). Code that uses this or calls global functions usually needs rewriting too.
Does it handle SVG?
Yes. Hyphenated SVG attributes such as stroke-width, fill-rule and clip-path become strokeWidth, fillRule and clipPath, and namespaced ones such as xlink:href become xlinkHref. Case-sensitive names like viewBox are kept.
What if my HTML has more than one top-level element?
A component can only return one root, so the output is wrapped in a React fragment (<>...</>) automatically.
Why does React warn about value or checked after converting?
The converter keeps value and checked as written. In React those make an input controlled, so without an onChange handler the field is read-only and React logs a warning. Rename them to defaultValue and defaultChecked if you just want a starting value.
Are curly braces in text handled?
Yes. Text like a {b} c becomes a {"{"}b{"}"} c so the braces display instead of being read as JavaScript. Script and style contents, and multi-line pre blocks, are wrapped in template literals so their whitespace survives.