HTML to JSX
Convert HTML into JSX for React. Attributes are renamed to their React props, such as class to className and for to htmlFor, inline styles become style objects, void elements are self-closed, comments become JSX comments and several root elements are wrapped in a fragment. You can also wrap the result in a function component.
Frequently asked questions
Which attributes are renamed?
class, for, tabindex, readonly, maxlength, colspan, rowspan, crossorigin, autocomplete, autofocus, srcset, datetime, contenteditable, spellcheck and the other HTML attributes React spells in camelCase. data- and aria- attributes stay as they are. Inside SVG, hyphenated attributes such as stroke-width become strokeWidth.
How are inline styles converted?
The style attribute becomes a style object with camelCase property names and string values, so background-color: red becomes backgroundColor: "red". Vendor prefixes become WebkitTransition and msTransform, and custom properties such as --gap keep their name in quotes.
What happens to inline event handlers?
An attribute such as onclick becomes onClick with an arrow function that runs the original code. Check those handlers, because code that relies on this or on global variables may need changes in React.
Why do value and checked become defaultValue and defaultChecked?
In React, value and checked make a field controlled, which needs an onChange handler. The HTML attributes set the starting value, which is what defaultValue and defaultChecked do, so the form keeps working as before. Checkbox and radio values stay as value.
How is whitespace handled?
Text inside an element is kept on one line with runs of spaces collapsed, which keeps the spaces between words and inline elements. Whitespace that only separates elements on different lines is dropped, as JSX does. The content of pre elements is kept exactly.
Are script and style elements kept?
Yes. Their content is written as a template literal inside the element, so the code or CSS is not parsed as JSX.