Working with HTML
JSX lets you write markup inside JavaScript that looks almost exactly like HTML, but it is not HTML. Each tag becomes a call that creates a React element, and attributes become properties of a JavaScript object. That is why a handful of rules differ from what you know from plain HTML. Learning these differences early saves you from confusing warnings and markup that silently does not work.
Attribute names follow JavaScript
Because class and for are reserved words in JavaScript, JSX uses className and htmlFor. Most other multi-word attributes and all event handlers are written in camelCase: tabIndex, maxLength, onClick, onChange. Two exceptions keep their dashes: data-* attributes and aria-* attributes stay exactly as in HTML.
function ProfileCard({ user }) {
return (
<>
{/* A fragment groups siblings without adding a wrapper div */}
<img src={user.avatar} alt={user.name} className="avatar" />
<label htmlFor="bio">Bio</label>
<textarea id="bio" defaultValue={user.bio} maxLength={200} />
<p
className="status"
style={{ color: user.online ? 'green' : 'gray', fontSize: '0.9rem' }}
data-user-id={user.id}
aria-live="polite"
>
{user.online ? 'Online' : 'Offline'}
</p>
<br />
</>
);
}
Every tag must be closed
HTML forgives unclosed tags like <br>, <img> and <input>. JSX does not. Void elements need a self-closing slash, as in <br />, and any component without children can be written the same way, such as <Spinner />. Forgetting the slash produces a compile error pointing at the next line, which can be puzzling at first.
One parent per return
A component returns a single expression, so two sibling elements must be wrapped. A div works, but it adds an extra node that can break CSS layouts such as flex rows or tables. A fragment, written <>...</>, groups siblings without adding anything to the page. Use the longer <Fragment key={...}> form when you need a key in a list.
Styles are objects, not strings
The style attribute takes a JavaScript object. CSS property names become camelCase, such as backgroundColor, and values are usually strings. The double braces in style={{ ... }} are not special syntax: the outer pair switches into JavaScript, and the inner pair is the object literal. A plain number is treated as pixels for most size properties, so { padding: 8 } means 8px.
Comments and other surprises
HTML comments do not work in JSX. Write {/* comment */} instead. Form fields also behave differently: in JSX, value on an input or textarea makes it controlled by React, while defaultValue sets only the starting text. Placing text between <textarea> tags, as you would in HTML, is not how React sets its content.
Finally, React escapes any string you place in braces, so {"<b>hi</b>"} shows the tags as text. That protects against injected scripts. Rendering raw HTML requires dangerouslySetInnerHTML, and its name is a deliberate warning to use it only with sanitised content.
Quick reference
classbecomesclassName;forbecomeshtmlFor.- Close every tag, including
<img />and<input />. - Wrap siblings in a fragment.
- Pass
stylean object with camelCase keys. - Write comments as
{/* ... */}.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.