HTML to JSX Converter
Paste HTML from a template, a design tool or an old page and get JSX or TSX that React accepts, with a note for everything that changed.
Körs i din webbläsare. Dina filer lämnar aldrig din enhet. Se hur du kontrollerar det
…
Så gör du
- 1Paste or open your HTML
Paste markup into the HTML box, or tap Open HTML file to load a .html or .svg file from your device.
- 2Choose the output
Pick JSX or TSX, type a component name that starts with a capital letter and choose the indent.
- 3Set the form options
Turn on Controlled inputs if your component will manage the values with state; leave it off for defaultValue.
- 4Read the notes
Check the warnings for removed event handlers or scripts and the counters for renamed attributes.
- 5Copy or download
Tap Copy, or Download to save a ready .jsx or .tsx file named after your component.
- 6Clean up in your editor
Replace removed handlers with real functions, split large parts into smaller components and run your formatter.
Om det här verktyget
JSX looks like HTML, but it is JavaScript underneath, and the differences bite the moment you paste a block of markup into a React file. The word class is reserved in JavaScript, so React wants className. The label attribute for becomes htmlFor. A style attribute must be an object with camelCase keys instead of a text string. Tags such as img, input and br must close themselves. HTML comments are a syntax error, and a stray curly brace in a sentence is read as the start of a JavaScript expression. Fixing all of that by hand in a long template is slow and easy to get wrong. This converter reads your HTML with a forgiving parser, the same way a browser tolerates unclosed paragraphs and list items, and writes clean JSX with proper indentation. It renames about seventy attributes that React spells differently, from tabindex and maxlength to the SVG presentation attributes such as stroke-width and fill-rule. It turns style strings into objects, keeps custom properties such as --gap as quoted keys, and gives numbers to unitless properties like z-index and opacity. Comments become JSX comments, braces in text are wrapped so they show as characters, and several top-level elements are wrapped in a fragment. You can wrap the result in a named component, choose JSX or TSX, and pick two or four spaces.
What the options do
Wrap in a component adds export default function with your component name around the markup, so the file is ready to import. TSX adds a JSX.Element return type. Controlled inputs keeps value and checked as they are; without it, they become defaultValue and defaultChecked, which is what React expects for a form whose state you do not manage yet. Keep inline event handlers turns onclick="save()" into onClick={() => { save(); }}; by default these handlers are removed and listed in the notes, because code inside a string rarely works unchanged in a component.
Things that need your attention
Script tags are kept as a string with a warning: React does not run them, so move that code into an effect or an import. A full HTML document is cut down to the contents of its body, since a component cannot render html or head. Textarea content becomes a defaultValue, and pre blocks keep their exact whitespace as a string expression. Entities such as & are decoded to real characters, while a non-breaking space stays visible as so you can still see it.
Limits
This is not a full HTML5 parser and it does not understand templating languages. Handlebars, Liquid, PHP or Blade tags in your input are treated as text, so convert the rendered output instead. It does not split a long page into several components or guess props for you, and it does not format with Prettier, although the output is already indented. Your markup never leaves the page: the conversion happens locally.
Lösta exempel
A signup form has a label for="email", an input with required, autocomplete and no closing slash, a button with style="padding: 8px 16px; font-weight: 600", an onsubmit handler and a sentence containing {plan}. The converter outputs htmlFor="email", autoComplete="email" with a self-closing input, style={{ padding: "8px 16px", fontWeight: 600 }} and the sentence with {"{"}plan{"}"}. The notes list one removed onsubmit handler, and the counters show 7 elements, 9 renamed attributes, 1 style object and 1 comment.
I korthet
- Kategori: Utvecklare & data
- Körs i webbläsaren, ingen registrering, ingen uppladdning.
- Uppgifterna du anger stannar på din enhet.
- Finns på engelska.
- Innehållet granskades senast:
- Officiell källa: React documentation: Writing markup with JSX and the list of DOM components and props
Vanliga frågor
Why does React use className instead of class?
JSX compiles to JavaScript function calls, and class is a reserved word in JavaScript. React therefore uses the DOM property name className. The same reason gives htmlFor instead of for. The converter renames both, plus the other attributes whose DOM property names differ.
What happens to inline styles?
A style string such as color: red; font-size: 12px becomes the object { color: "red", fontSize: "12px" }. Vendor prefixes follow React's rules (-webkit-transition becomes WebkitTransition, -ms-transform becomes msTransform) and custom properties such as --gap stay as quoted keys.
Can it make a TypeScript component?
Yes. Choose TSX and keep Wrap in a component on. The output is export default function YourName(): JSX.Element with the markup inside the return. Save it with the .tsx extension.
What about onclick and other inline handlers?
By default they are removed and listed in the notes, because React event handlers must be functions. Turn on Keep inline event handlers to get arrow functions such as onClick={() => { save(); }}, then replace them with real handlers in your component.
Does it work on SVG icons?
Yes. viewbox becomes viewBox, stroke-width becomes strokeWidth, xlink:href becomes xlinkHref and element names such as linearGradient and clipPath keep their capital letters. For whole SVG files with editor clutter, the separate SVG to React component tool also cleans the markup.
Is my code sent anywhere?
No. The parser and converter run in this page. Your last input and options are saved only in this browser so they are still there when you come back.
