How to convert HTML to JSX
JSX looks like HTML, but it is JavaScript, so a copy and paste from an HTML file fails in a few predictable places. To convert html to jsx, the tool reads your HTML, renames the attributes React expects, turns style strings into objects, closes the tags HTML leaves open and checks the result is valid JSX.
The built-in example, a signup card, converts to this. The status line reports 8 attributes renamed and 1 style converted:
<>
{/* Signup card */}
<div className="card" style={{ padding: "16px", backgroundColor: "white", opacity: 0.9 }}>
<h2 className="card__title" tabIndex={0}>Join us</h2>
<p>Read the <a href="/terms" target="_blank">terms</a> first.</p>
<label htmlFor="email">Email</label>
<input id="email" type="email" maxLength={80} required />
<input type="checkbox" defaultChecked /> Remember me
<br />
<svg viewBox="0 0 24 24" strokeWidth="2">
<path d="M5 12h14" strokeLinecap="round" />
</svg>
</div>
</>What changes when you convert HTML to JSX
- Attribute names.
classbecomesclassNameandforbecomeshtmlFor. Names such as tabindex, colspan, maxlength, readonly and autofocus take their camelCase form. - Style attributes. A string like
padding: 16px; background-color: whitebecomes an object with camelCase keys. Unitless numbers such as opacity become real numbers. - Void tags.
<br>,<img>,<input>and<hr>get a closing slash. - Comments. An HTML comment becomes a JSX comment in braces.
- Form fields.
valueon a text input becomesdefaultValue, andcheckedbecomesdefaultChecked, so React does not treat the field as controlled. A selected option moves todefaultValueon its select, and the text of a textarea moves to itsdefaultValue. - Tables. Rows placed straight inside a table are wrapped in a
tbody, as a browser would, because React warns about them otherwise. - Special characters. Braces and angle brackets in text are written as string expressions, since they have a meaning in JSX.
HTML to JSX for SVG
Inline SVG has its own attribute names, written with hyphens in HTML and in camelCase in JSX: stroke-width becomes strokeWidth, stroke-linecap becomes strokeLinecap, and xlink:href becomes xlinkHref. Names that are already camelCase in SVG, such as viewBox, are kept. If you only have an SVG file, SVG to JSX is built for it and also offers component and TypeScript output.
Event attributes: onclick and friends
In HTML, onclick="save()" is a string of code. In React, onClick takes a function, so the string cannot be passed along. By default the tool removes these attributes and lists them so you know where to add handlers. Choose Wrap in a function and each one becomes onClick={() => { save() }}. That is a starting point only, because code written for HTML often relies on globals, this or the old event object.
Script tags are kept as written but React does not run them. Move that code into handlers or effects.
HTML to JSX component output
Pick Function component for export default function Name() { return (...); }, or Arrow function component for a const Name = () => (...) followed by a default export. The name is turned into a valid PascalCase identifier. If your HTML has more than one top-level element, the result is wrapped in a fragment, <>...</>, because a component must return one root.
To go back, use JSX to HTML, which turns className, style objects and other JSX details into plain HTML. To clean up HTML before converting, HTML Encoder and HTML Decoder handle entities, and Remove HTML Tags strips markup you only need as text.
Line breaks and spacing
JSX ignores a line break together with the spaces around it. The converter follows that rule: text and inline tags that sit on one line in your HTML stay on one line in the JSX, with their spaces. A line break between two tags is treated as formatting and is not kept as a space. Preformatted text in pre keeps every space and line break, written as a template string.
Limits
- The tool checks syntax, not behavior. Open the JSX in your project to catch props React rejects.
- Whole pages lose their html, body and head tags, and so do the title, meta and link tags, since a component cannot contain them.
- Attributes written with unusual capitals are matched in lowercase, except inside SVG.
- Inline event code is not translated to React code.
Your HTML is converted in your browser. It is not uploaded or stored. See the Privacy Policy for details.