To add comments, attributes, and inline styles in JSX, you need three small rules. Each one translates a piece of HTML into JavaScript, and the three rules all come from one fact: JSX compiles to JavaScript.
Comments clarify intent, attributes configure elements, and inline styles set visual values. Once you know the rules, the rest of JSX reads like JavaScript you already know.
Here is a comment inside a component:
function App() {
return (
<div>
{/* This is a JSX comment. */}
<h1>Dashboard</h1>
</div>
);
}The comment does not appear on screen. It sits inside the JSX where a future reader will find it, which is the point.
Add a comment in JSX
Inside JSX, a comment must be wrapped in curly braces with an asterisk. The braces tell React to treat the contents as JavaScript, and the asterisk makes it a JavaScript block comment.
Outside the JSX, in the body of the component, a regular JavaScript comment with two slashes works as usual. The special syntax only matters between the tags.
Comments are for people, not the computer, so keep them short and explain why rather than what. You will most often comment out a line while testing, or leave a note for the next developer.
Write attributes in camelCase
HTML attributes are lowercase, but JSX turns most of them into camelCase because they become JavaScript object properties. The reserved word class becomes className, and for becomes htmlFor.
function EmailField() {
return (
<label htmlFor="email">
Email
<input id="email" maxLength={40} aria-describedby="hint" />
</label>
);
}The label now associates with the input through htmlFor, and the input enforces a 40 character limit. The aria attribute keeps its hyphen, because aria and data attributes are the two groups that do not switch to camelCase.
When you mistype an attribute, React prints a warning in the browser console with the correct spelling. A quoted attribute is a string, while a brace-wrapped attribute reads a JavaScript value.
Apply inline styles as objects
Inline styles in JSX are JavaScript objects, not strings. Property names use camelCase, so background-color becomes backgroundColor.
function Banner() {
return (
<p style={{ backgroundColor: "crimson", color: "white" }}>
Sale ends tonight.
</p>
);
}The paragraph renders with a crimson background and white text. The outer braces open the JavaScript expression, and the inner braces are the style object itself.
The same object shape works for any style property, from margin to lineHeight. Most of the time a CSS class is cleaner than a style object, so reach for inline styles only when the value depends on data or state.
For the full attribute comparison, see the guide on JSX versus HTML. The braces themselves are covered in the guide on rendering variables and expressions in JSX.
Rune AI
Key Insights
- JSX comments live inside curly braces with an asterisk.
- Attributes use camelCase, so class becomes className.
- aria and data attributes keep their hyphens.
- Inline styles are objects with camelCase property names.
- The double braces are an object inside an expression.
Frequently Asked Questions
How do I write a comment inside JSX?
Why is the class attribute called className?
Why do inline styles use two sets of curly braces?
Conclusion
Comments, attributes, and inline styles each translate from HTML into JSX with one small change. Comments move inside braces, attributes switch to camelCase, and styles become objects. Once you know the three rules, the rest of JSX reads naturally.
More in this topic
How to Build a Dropdown Menu in React
Build a React dropdown menu with the ARIA menu button pattern. Handle open and close, keyboard arrows, and clicks outside the menu.
How to Animate React Components with Motion
Animate React components with the Motion library. Set up motion, add enter, hover, and exit animations, and respect reduced motion.
Headless UI Components Explained: Logic Without Locked Styling
Understand headless UI components and how libraries like Radix give you unstyled, accessible behavior that you style yourself.