React JS: from HTML to JSX
HTML for React (4/5)
1 Overview
📘 HTML to JSX JSX is a syntax extension for JavaScript that lets us write HTML-like markup inside a component. It looks like HTML but it is JavaScript: Babel/Vite turn every tag into a function call (React.createElement / jsx()), and React renders real HTML elements into the DOM.
Because it is JavaScript, a few HTML habits must change: reserved words, camelCase names, functions instead of strings, objects instead of CSS strings, and every tag closed.
2 HTML vs JSX
JSX looks like HTML, but it is JavaScript, so a few things change:
| HTML | React JSX |
|---|---|
class="card" |
className="card" |
for="name" |
htmlFor="name" |
onclick="doIt()" (string) |
onClick={doIt} (a function) |
onchange, onsubmit |
onChange, onSubmit (camelCase) |
style="color:red" |
style={{ color: "red" }} (an object) |
<br>, <img src="a.png"> |
<br />, <img src="a.png" /> |
return false / preventDefault() |
always event.preventDefault() |
onClick={handleClick} ✅ passes the function and React calls it on click. onClick={handleClick()} ❌ calls it immediately on every render. If you need arguments, wrap it: onClick={() => handleClick(id)}.
3 className and htmlFor
class and for are reserved words in JavaScript (class Person {}, for (...)), so JSX uses the DOM property names instead:
Field.jsx
The browser still receives class="field" and for="email".
4 Events: camelCase and functions
| HTML | JSX |
|---|---|
<button onclick="save()"> |
<button onClick={save}> |
<input onchange="check(this)"> |
<input onChange={(e) => check(e.target)} /> |
<form onsubmit="return send()"> |
<form onSubmit={handleSubmit}> |
<div onmouseover="..."> |
<div onMouseOver={...}> |
- Event names are camelCase:
onClick,onChange,onSubmit,onKeyDown,onMouseOver. - The value is a function between curly braces, not a string of code.
return falsedoes not cancel anything in React: callevent.preventDefault().- In React
onChangefires on every keystroke (like the HTMLinputevent).
5 The style attribute is an object
In HTML style is a CSS string. In JSX it is a JavaScript object with camelCase property names:
Badge.jsx
// HTML: <span style="background-color: gold; font-size: 14px">New</span>
const badgeStyle = { backgroundColor: "gold", fontSize: 14, padding: "2px 6px" };
export default function Badge() {
return <span style={badgeStyle}>New</span>;
}
// or inline: the outer braces are JSX, the inner braces are the object
<span style={{ backgroundColor: "gold", fontSize: 14 }}>New</span>Numbers without units are treated as px (fontSize: 14 → 14px). For most styling, prefer CSS classes: see CSS basics for React.
6 Close every tag
HTML forgives <br>, <img>, <input>, <hr> and even an unclosed <li>. JSX doesn’t:
7 One root element and fragments
A component must return a single root element. If you don’t want an extra <div> in the DOM, use a fragment <>...</>:
Header.jsx
When you render a list of fragments with map(), use the long form so you can pass a key: <Fragment key={item.id}>...</Fragment>.
8 JavaScript in curly braces
Inside JSX, { } opens a window to JavaScript: variables, expressions, function calls, map(), ternaries.
List.jsx
HTML comments <!-- --> don’t work in JSX. Use {/* comment */}.
9 Full example: a signup form
The signup form from HTML forms, now as a React component with state and events:
SignupForm.jsx
import { useState } from "react";
export default function SignupForm() {
const [name, setName] = useState("");
const [message, setMessage] = useState("");
function handleSubmit(event) {
event.preventDefault(); // same as in plain JS: no page reload
setMessage(`Welcome, ${name}!`);
}
return (
<form className="signup" onSubmit={handleSubmit}>
<label htmlFor="name">Name</label>
<input
id="name"
type="text"
value={name}
onChange={(event) => setName(event.target.value)}
/>
<button type="submit" onMouseOver={() => console.log("hover")}>
Send
</button>
{message && <p>{message}</p>}
</form>
);
}onSubmit,onChangeandonMouseOverare the same DOM events, written in camelCase and receiving a function.- The handler receives an event object (React wraps the native one in a SyntheticEvent with the same API:
target,key,preventDefault(),stopPropagation()). - In React
onChangefires on every keystroke, like the HTMLinputevent. - We don’t use
getElementByIdoraddEventListener: React updates the DOM for us when the state changes.
More about JSX in React JS: JSX, about components in React JS: Components and about state in React JS: useState.
1. HTML basics · 2. Forms · 3. Events and the DOM · 4. From HTML to JSX · 5. CSS basics
⬅️ Previous: Events and the DOM · Next ➡️ CSS basics for React