Writing JSX Code in Components
A React component is a JavaScript function that returns JSX, the HTML-like syntax that describes what should appear on screen. Writing JSX inside components is how you build every part of a React interface, from a single button to a full page. Once you know a few rules, JSX feels like writing HTML with the power of JavaScript mixed in.
The shape of a component
A component name must start with a capital letter. React uses that to tell your components apart from built-in tags: <Header /> means "render my component", while <header> means "create an HTML header element". The function returns JSX, and the file usually exports the component so others can import it.
function CourseCard() {
const title = "React Basics";
const lessons = 12;
const started = new Date().toLocaleDateString();
return (
<article className="course-card">
<h2>{title}</h2>
<p>{lessons} lessons, about {lessons * 15} minutes</p>
<p>Viewed on {started}</p>
<label htmlFor="notes">Notes</label>
<textarea id="notes" />
</article>
);
}
export default CourseCard;
Rules JSX enforces
- One root element: a component must return a single parent. If you do not want an extra
div, use a fragment:<> ... </>. - Close every tag: void elements need a slash, so write
<img />,<br />and<input />. - camelCase attributes:
classNameinstead ofclass,htmlForinstead offor,onClickinstead ofonclick,tabIndexinstead oftabindex. - Curly braces for JavaScript: anything inside
{ }is evaluated as a JavaScript expression.
Expressions, not statements
Curly braces accept expressions, meaning anything that produces a value: variables, maths, function calls, ternaries and array methods like map. They do not accept statements such as if, for or variable declarations. If you need an if, do the work above the return and store the result in a variable, then drop that variable into the JSX.
Why the parentheses after return matter
When JSX spans several lines, wrap it in parentheses. JavaScript automatically inserts a semicolon after a bare return at the end of a line. Without the brackets, writing return and starting the JSX on the next line means the function returns undefined and nothing renders, with no obvious error message. The parentheses keep the whole JSX block attached to the return.
What JSX turns into
Browsers do not understand JSX. Your build tool converts each tag into a function call that creates a lightweight object describing the element. React then compares those objects with the previous render and updates only what changed in the real DOM. This is why JSX values must be expressions: each tag is itself an expression that produces a value.
Splitting a component that grows
When a component's JSX gets long, look for repeated or self-contained chunks and move them into their own components. A good sign is a block you would describe with its own noun, such as "price tag" or "author line". Smaller components are easier to read, reuse and style.
Takeaway: capitalise component names, return one root element wrapped in parentheses, close every tag, use camelCase attributes, and put only expressions inside curly braces.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.