Loading Techie Utils…

HTML to JSX Converter

Convert HTML and SVG markup into JSX for React, with styles, attributes and whitespace handled. Free, and runs in your browser.

About HTML to JSX

JSX looks like HTML and follows different rules. Most of the conversion is renaming: class becomes className, for becomes htmlFor, tabindex becomes tabIndex, and SVG's stroke-width becomes strokeWidth. The renaming is the easy part.

The parts that break pasted markup are handled here. Void elements such as <br> are closed. A style string becomes a style object, keeping custom properties and vendor prefixes. Inline onclick handlers become arrow functions. Braces and angle brackets in text are escaped so they cannot open an expression. Comments become {/* */}. Several top-level elements are wrapped in a fragment.

Whitespace is the subtle one. JSX drops whitespace that contains a line break and keeps whitespace that does not. The space between two inline elements on separate lines has to be written as {" "}, or the words run together. The converter adds it where HTML would have rendered a space.

Where it helps

  • Renames all the HTML and SVG attributes React spells differently, including SVG's hyphenated presentation attributes and xlink:href.
  • Converts style strings to objects correctly, including custom properties, vendor prefixes and semicolons inside url(…).
  • Handles JSX whitespace, adding {" "} where a space would otherwise vanish between inline elements.
  • Keeps the case of SVG tag names such as linearGradient and clipPath, which a DOM-based converter would lowercase.
  • Runs in the browser, so markup from an unreleased project is not sent anywhere.

Limits

  • Inline event handlers become arrow functions around the same code, but they no longer run with this bound to the element. Any handler that uses this needs rewriting.
  • It converts markup, not behaviour. Scripts, forms that post and anything that relied on the DOM being HTML will still need to be moved into React's way of doing things.
  • Malformed HTML is repaired the simple way, by closing open elements at the next matching end tag. Deeply broken markup is best run through the HTML formatter first.

Frequently asked questions

Why does React need className instead of class?

JSX compiles to JavaScript object properties, and early JavaScript did not allow reserved words such as class and for as property names. React named the props after the DOM properties, className and htmlFor, and kept those names.

Why did value become defaultValue?

In React, an input with value and no onChange handler is controlled and read-only: typing does nothing. defaultValue sets the starting value and lets the field be edited, which is what the HTML did. To control the field, change it back to value and add an onChange handler.

What is the {" "} in the output?

An explicit space. JSX removes whitespace that contains a line break, so when inline elements sit on separate lines, the space between them would disappear. Writing it as an expression keeps it.

Does the output work in TypeScript?

Yes, the markup is valid in a .tsx file as it is. The only thing TypeScript may ask for is a type on the event parameter of a converted handler, such as React.MouseEvent<HTMLButtonElement>.

Related tools

More Web Development tools