HTML to JSX Converter
Convert an HTML fragment to JSX that compiles in React.
About HTML to JSX Converter
An HTML to JSX converter that renames attributes, turns inline styles into objects, closes void elements and escapes braces so the markup compiles in React.
How to use
- Paste an HTML fragment or load an
.htmlfile. The JSX updates as you type. - Turn Keep comments off to drop HTML comments instead of converting them.
- Read any notes under the output: they list what was left out or needs a second look.
- Copy the JSX or download it as
.jsx.
What it does
- Renames attributes:
classtoclassName,fortohtmlFor, and the rest to React's camelCase, such astabindextotabIndex,maxlengthtomaxLengthandsrcsettosrcSet.data-andaria-attributes are unchanged. - Writes boolean attributes bare (
disabled="disabled"becomesdisabled) and whole-number values oftabIndex,colSpan,rowSpan,maxLength,rowsand similar as numbers. - Turns
style="margin-top: 8px"intostyle={{ marginTop: '8px' }}, and wraps an inline handler such asonclick="send()"in an arrow function underonClick. - On inputs,
valueandcheckedbecomedefaultValueanddefaultChecked, and a textarea's text becomesdefaultValue, so the fields stay editable. - Self-closes void and empty elements, converts comments to
{/* */}, escapes{and}in text, and wraps several top-level nodes in a fragment. - Collapses whitespace as a browser does. Indentation between tags is dropped, a space that would show on screen is kept, and text inside
<pre>is preserved exactly. The contents of<style>and<script>go into a template literal. - Custom elements keep their attributes as written, and an inline
<svg>is converted with the SVG rules. - The input is read by the browser's HTML parser, so invalid markup is repaired the way a browser repairs it, and
<html>,<head>and<body>tags are dropped while their contents stay. Attribute names JSX cannot express, such as@clickand:class, are left out and listed.selectedon an option is left as it is, the contents of a nested<template>are not converted, long lines of text are not wrapped, and no component is generated around the result.
Example
<label for="email" class="field">Email</label>
<input id="email" type="email" maxlength="80" required>
<button onclick="send()" style="margin-top: 8px">Send</button>
→
<>
<label htmlFor="email" className="field">Email</label>
<input id="email" type="email" maxLength={80} required />
<button onClick={() => { send() }} style={{ marginTop: '8px' }}>Send</button>
</>
Privacy
The conversion runs in the browser. The HTML is parsed into an inert document, so its scripts do not run and its images are not fetched. No request is made, and nothing is stored.
Frequently asked questions
- What is the difference between HTML and JSX?
- JSX is JavaScript syntax that looks like HTML. Attributes are props named in camelCase, class and for are written className and htmlFor, every element must be closed, style takes an object, and anything in braces is a JavaScript expression.
- Why does React use className instead of class?
- JSX props are named after DOM properties, and the DOM property for the class attribute is className, because class is a reserved word in JavaScript. The same reasoning gives htmlFor for the for attribute.
- How do I write inline styles in JSX?
- Pass an object with camelCase property names: style={{ marginTop: '8px', fontWeight: 600 }}. A bare number gets px added, except for unitless properties such as opacity, zIndex, flex and lineHeight.
- How do I write a comment in JSX?
- Put a JavaScript block comment inside braces: {/* like this */}. An HTML comment written as <!-- --> is a syntax error in JSX.