Skip to main content

Components are functions

A React component is a JavaScript function that returns JSX — a description of what should appear on screen.
That’s a complete React component. A function. Returns JSX. Done.

Rules for components

  1. Name starts with a capital letterUserCard, not userCard
  2. Returns JSX — the UI description
  3. One component per file (convention, not required)
  4. Export it so other files can use it
Component names must start with a capital letter. <UserCard /> renders your component. <userCard /> is treated as an HTML tag (which doesn’t exist) and silently fails.

What is JSX?

JSX is a syntax extension that lets you write HTML-like code inside JavaScript. It’s not actual HTML — it gets compiled to JavaScript function calls.
You never write the React.createElement version. JSX does it for you. But knowing it’s just JavaScript under the hood helps explain the rules.

JSX rules

1. Return a single root element

<>...</> is a Fragment — it groups elements without adding an extra DOM node. Use it when you don’t need a wrapper div.

2. className instead of class

3. camelCase for attributes

4. Close all tags

5. JavaScript expressions in curly braces

Use {} to embed any JavaScript expression inside JSX:
Inside {} you can use:
  • Variables: {name}
  • Expressions: {count + 1}
  • Function calls: {formatDate(date)}
  • Ternary operators: {isActive ? "Yes" : "No"}
  • Template literals: {`Hello, ${name}`}
You can only use expressions inside {}, not statements. {if (x) ...} won’t work. Use ternary operators or && instead. {x ? "yes" : "no"} and {x && "shown"} are the React way.

Inline styles

JSX styles use a JavaScript object with camelCase properties:
The double braces {{ }} are: outer {} for “this is JavaScript”, inner {} for “this is an object.”
Inline styles work but aren’t ideal for large apps. For most projects, use CSS files (import './App.css'), CSS modules, or a library like Tailwind CSS. Inline styles are fine for dynamic values like width: ${percentage}%.

JSX vs HTML cheat sheet

Common mistakes

A component must return a single root element. Use <>...</> (Fragment) when you don’t need an actual wrapper div in the DOM.
This is the most common JSX mistake. React will show a warning in the console, but it’s easy to miss.

What’s next?

Components are reusable, but right now they always show the same content. Let’s make them dynamic by passing data in with props.

Props

Pass data between React components