Components are functions
A React component is a JavaScript function that returns JSX — a description of what should appear on screen.Rules for components
- Name starts with a capital letter —
UserCard, notuserCard - Returns JSX — the UI description
- One component per file (convention, not required)
- Export it so other files can use it
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.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:
{} you can use:
- Variables:
{name} - Expressions:
{count + 1} - Function calls:
{formatDate(date)} - Ternary operators:
{isActive ? "Yes" : "No"} - Template literals:
{`Hello, ${name}`}
Inline styles
JSX styles use a JavaScript object with camelCase properties:{{ }} are: outer {} for “this is JavaScript”, inner {} for “this is an object.”
JSX vs HTML cheat sheet
Common mistakes
Returning multiple elements without a wrapper
Returning multiple elements without a wrapper
Using class instead of className
Using class instead of className
Forgetting curly braces for JavaScript
Forgetting curly braces for JavaScript
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