REACT / LESSON 02
Components & JSX.
Learn how to build React components and write JSX, the special syntax that combines HTML with JavaScript

Loading the lesson…
Loading activities…
REACT / LESSON 02
Learn how to build React components and write JSX, the special syntax that combines HTML with JavaScript

Loading the lesson…
Loading activities…
Think of a React component like a cookie cutter. Design the shape once and you can stamp out as many cookies as you want. Every cookie (that's a component instance) has the same shape, but each one can get different decorations (that's the data). Components are the building blocks of every single React app.
A React component is just a JavaScript function that returns JSX (which looks like HTML). Seriously, that's it! Here's the simplest component you can write.
Three things make that a valid React component.
<Greeting /> anywhere in your appJSX stands for JavaScript XML, and it lets you write HTML-looking code right inside your JavaScript. Under the hood it gets compiled down to regular JavaScript. The HTML-looking version is just WAY easier for humans to read and write.
You'll never write that _jsx version yourself. Your build tool adds it for you, import line and all (which is also why you don't need import React from 'react' at the top of every file anymore). Older tutorials show React.createElement here instead, since that's what JSX used to compile to. Either way the takeaway is the same... JSX is really just a friendlier way to create elements.
JSX has a few rules that are a lil different from HTML. Here they are.
The word class is already reserved in JavaScript, so JSX uses className for CSS classes instead.
Every tag has to be closed in JSX. That includes self-closing tags like <img /> and <br />, which plain HTML lets you get away with leaving open.
A component can only return one top-level element. If you've got more than one, wrap them in a <div> or a Fragment (<>...</>).
Here's where JSX gets fun. You can drop JavaScript expressions right into it using curly braces { }. Think of curly braces as an escape hatch out of HTML-land and back into JavaScript-land.
Pretty much any JavaScript expression can go inside curly braces.
{name}{2 + 2}{name.toUpperCase()}{isOnline ? "Online" : "Offline"}The real magic happens when you start putting components inside other components. That's called composition, and it's how you build big complicated UIs out of small simple pieces.
See how App uses UserInfo, which in turn uses Avatar? Each component is small and focused on one job. So if you want to change how avatars look, you update the Avatar component ONE time, and every place that uses it updates automatically.
Sometimes you need to return several elements but you don't want an extra <div> showing up on the page. That's exactly what Fragments are for.
Those empty tags <>...</> group elements together without adding any HTML to the page at all. They come in clutch inside tables, lists and flex containers, where one extra <div> would wreck your layout.
Alright, here's a more realistic component that pulls everything together.
That one component can display any product and adjust how it looks based on whether it's in stock. You could use it 100 times on a page with 100 different products (and you'd only ever fix bugs in one spot... wholesome right?)
className instead of class, and needs every tag closed{ } to drop JavaScript expressions into JSX<>...</>) group elements without adding extra DOM nodesRight now your components are static. They show the same thing every time. In the next lesson you'll learn how to give them a memory with State, so they can actually react when somebody clicks something...