Components and JSX
React is a JavaScript library for building user interfaces out of components: small, reusable pieces of UI. Facebook, Instagram, Netflix, Airbnb and countless startups build with it, and it's the foundation of Next.js and React Native.
Prerequisites: comfortable JavaScript (functions, arrays, objects, arrow functions, map). If you're shaky on those, do the JavaScript track first.
Why React?
Without React, you update the page by hand: find an element, change its text, add a class, remove a node… and keep all of that in sync with your data. React flips it around:
UI = f(state). You describe what the UI should look like for the current data, and React updates the page for you whenever the data changes.
Your first component
A component is just a function that returns JSX (HTML-like markup):
The preview updates live as you type. Try changing the text.
In this editor: whatever component you name
Appis rendered in the preview. The editor runs React 19, the same version you'd install today.
Composing components
Components can use other components, like custom HTML tags. Component names must start with a capital letter (lowercase names are treated as HTML elements).
This is React's superpower: build small pieces, then assemble them into pages and apps.
JSX rules
JSX looks like HTML, but it's actually JavaScript, so there are a few differences:
| Rule | HTML | JSX |
|---|---|---|
| Return one root element | — | Wrap siblings in <div> or a fragment <>…</> |
| Close every tag | <img> <br> |
<img /> <br /> |
class attribute |
class="btn" |
className="btn" |
for attribute |
for="email" |
htmlFor="email" |
| Attribute names | onclick, tabindex |
camelCase: onClick, tabIndex |
| Inline styles | style="color: red" |
style={{ color: 'red' }} (an object) |
| Comments | <!-- --> |
{/* comment */} |
JavaScript inside JSX: { }
Curly braces let you put any JavaScript expression into your markup:
You can use expressions (values, function calls, ternaries) in braces, but not statements like if or for. You'll see how to handle conditions and lists in later lessons.
What JSX really is
JSX is compiled into plain function calls. This:
<h1 className="title">Hello</h1>becomes roughly:
jsx('h1', { className: 'title', children: 'Hello' })Those calls create lightweight objects describing the UI. React compares them with the previous render and updates only the parts of the real page (the DOM) that changed.
Setting up a real React project
npm create vite@latest my-app -- --template react-ts # React + TypeScript
cd my-app
npm install
npm run devFor full-stack apps with routing and server rendering, use Next.js (it has its own track).
Practice
Build a ProfileCard component that shows a name, a job title and a short bio from variables, with a "Follow" button. Render three of them from App, each with a different name. (Hint: you'll pass data properly in the next lesson; for now, make three components or hard-code the text.)
1.Why must React component names start with a capital letter?
2.How do you set a CSS class in JSX?