DevToolBoxGRATIS
Blog

HTML naar JSX: Alles wat je nodig hebt voor React-migratie

10 min lezenby DevToolBox

Migrating HTML templates to React means rewriting your markup as JSX. While JSX looks similar to HTML, there are critical differences that will cause compilation errors if overlooked. This guide covers every change you need to make.

Convert HTML to JSX automatically with our free tool β†’

Why JSX Is Different from HTML

JSX is not HTML. It's a syntax extension for JavaScript that compiles to React.createElement() calls. Because JSX lives inside JavaScript, it follows JavaScript rules β€” and several HTML attribute names clash with JavaScript reserved words.

The compiler will throw errors if you use standard HTML attributes like class or for in JSX. Understanding these differences is essential for any React migration.

Key Attribute Differences

These are the most common attributes that must change when converting HTML to JSX:

HTMLJSXReason
classclassNameclass is a reserved JS keyword
forhtmlForfor is a reserved JS keyword
tabindextabIndexJSX uses camelCase
readonlyreadOnlyJSX uses camelCase
maxlengthmaxLengthJSX uses camelCase
onclickonClickJSX uses camelCase
style="color: red"style={{ color: "red" }}JSX style is an object

The Style Attribute

In HTML, style is a string. In JSX, it's a JavaScript object with camelCase property names:

<!-- HTML -->
<div style="background-color: #f0f0f0; font-size: 14px; margin-top: 20px;">
  Hello
</div>

// JSX
<div style={{ backgroundColor: "#f0f0f0", fontSize: "14px", marginTop: "20px" }}>
  Hello
</div>

All CSS property names must be camelCased: background-color becomes backgroundColor, font-size becomes fontSize, z-index becomes zIndex.

Self-Closing Tags

HTML allows some tags to be unclosed (void elements). In JSX, every tag must be explicitly closed:

  • <br> β†’ <br />
  • <hr> β†’ <hr />
  • <img src="..."> β†’ <img src="..." />
  • <input type="text"> β†’ <input type="text" />
  • <meta charset="utf-8"> β†’ <meta charSet="utf-8" />
<!-- HTML -->
<img src="photo.jpg" alt="Photo">
<input type="email" placeholder="Email">
<br>

// JSX
<img src="photo.jpg" alt="Photo" />
<input type="email" placeholder="Email" />
<br />

Event Handlers

HTML uses lowercase event attributes as strings. JSX uses camelCase with function references:

<!-- HTML -->
<button onclick="handleClick()">Click me</button>
<input onchange="handleChange()" onfocus="handleFocus()">

// JSX
<button onClick={handleClick}>Click me</button>
<input onChange={handleChange} onFocus={handleFocus} />

In JSX, event handlers receive a SyntheticEvent, not a native DOM event. React normalizes events across browsers for consistent behavior.

Comments in JSX

HTML comments (<!-- ... -->) are not valid in JSX. Use JavaScript block comments wrapped in curly braces:

<!-- HTML comment -->
<div>Hello</div>

// JSX comment
<div>
  {/* This is a JSX comment */}
  Hello
</div>

Common Pitfalls

  • Forgotten className β€” the most common migration error. Search and replace all class= with className=
  • Inline styles as strings β€” style="margin: 10px" will throw an error; use an object instead
  • Unclosed tags β€” <img> or <br> without self-closing slash will fail
  • Adjacent elements β€” JSX must return a single root element; wrap siblings in <>...</> (Fragment)
  • Boolean attributes β€” HTML disabled becomes JSX disabled (same) or explicitly disabled={true}
  • Curly braces in text β€” literal { and } must be expressed as {'{'} and {'}'}
// Fragment wrapper for multiple elements
function App() {
  return (
    <>
      <Header />
      <Main />
      <Footer />
    </>
  );
}

Automate Your Migration

Manually converting large HTML files to JSX is error-prone. Our HTML to JSX converter handles all the transformations automatically β€” class to className, style strings to objects, self-closing tags, and more.

Try the HTML to JSX converter now β†’

𝕏 Twitterin LinkedIn
Was dit nuttig?

Blijf op de hoogte

Ontvang wekelijkse dev-tips en nieuwe tools.

Geen spam. Altijd opzegbaar.

Try These Related Tools

JSXHTML to JSXSVGSVG to JSX/ReactTWCSS to TailwindJSJS/HTML Formatter

Related Articles

SVG in React: Van ruwe SVG naar geoptimaliseerde componenten

Leer hoe je ruwe SVG omzet naar efficiΓ«nte React-componenten. SVGO-optimalisatie, toegankelijkheid, animaties en TypeScript-props.

Migreren van CSS naar Tailwind: Een praktische gids

Stapsgewijze gids voor het migreren van traditionele CSS naar Tailwind CSS. Utility-mapping, responsive design, aangepaste thema's en prestatietips.

SVG to JSX: Convert SVG for React Components (Complete Guide)

Learn how to convert SVG to JSX for React. Covers attribute differences, automated conversion, dynamic props, SVGO optimization, and accessibility best practices.