JSX looks like HTML, but it lives inside JavaScript. That has two big consequences. First, you can put JavaScript values straight into it. Second, it has a few rules that HTML doesn't, and breaking them stops the app from compiling.
Curly braces: JavaScript inside JSX
Inside JSX, anything between { and } is a JavaScript expression: a
piece of code that produces a value. Strings and numbers render as text;
JSX elements render as elements. null, undefined and booleans show nothing.
A plain object cannot be displayed directly: choose one of its fields instead.
Read the lab below and predict the three lines it shows before you run it. Work out the arithmetic on paper.
npm install and npm run dev. Check your prediction
Welcome, Ada!
You plan to study for 315 minutes this week.
That is 5 hours, rounded.minutesPerDay * days is 45 * 7, which is 315. Then 315 / 60 is 5.25,
and Math.round turns that into 5.
A few things to notice:
-
The variables are declared outside the
Appfunction, as ordinary JavaScript. The JSX just refers to them:JSXconst learner = "Ada"; const minutesPerDay = 45; const days = 7; export default function App() { return ( <main> <h1>Welcome, {learner}!</h1> <p> You plan to study for {minutesPerDay * days} minutes this week. </p> <p>That is {Math.round((minutesPerDay * days) / 60)} hours, rounded.</p> </main> ); } -
Braces hold expressions, not statements.
{minutesPerDay * days}works.{let total = 315}does not, becauseletis a statement. If you need a statement, put it above thereturn. -
Text outside braces is JSX text. Spaces between words matter, but line breaks and indentation are normalized rather than copied exactly. Use
{" "}when you need an explicit space between adjacent expressions or elements.
Change days to 5 and run the lab again. Only the numbers change, because
only the values changed.
Three rules HTML doesn't have
1. Return one parent element
A component returns one JSX element. That element can contain as many
others as you like, but you can't return two siblings side by side. If you have
nothing sensible to wrap them in, use an empty tag, <> and </>. This is
called a fragment. It groups elements without adding anything to the page.
2. Close every tag
In HTML, <img>, <input> and <br> don't need closing tags. In JSX every
element must be closed. An element with no children closes itself with />,
as in <input placeholder="..." /> or <br />.
3. Use className, not class
React's built-in elements use className for the HTML class attribute,
and htmlFor for a label's HTML for attribute. These are React's prop names;
class="card" can still compile as JSX, but React warns that the prop should
be className. A warning and a syntax error are different problems.
Repair the broken component
This component breaks all three rules. Press Run and read the console. The
message starts with the file and the position of the problem, such as
src/App.jsx:4:5: line 4, column 5. Under it, the lab adds a hint in plain
English.
npm install and npm run dev. Hint 1: read the first error
The message is about adjacent JSX elements, and it points at line 4, where
<p begins. The h1 on line 3 and the p on line 4 sit side by side, with no
parent element around them. When an error points at the start of an element,
look at what comes just before it, too.
Hint 2: fix one rule at a time
Wrap the three elements in <> and </>, then run again. The compiler moves
on and complains about the next problem. Fix that, run again, and repeat. Once
the app runs, check the console for a warning React shows while it runs,
not while it compiles.
Show the repaired component
export default function App() {
return (
<>
<h1>Study notes</h1>
<p className="note">React components return JSX.</p>
<input placeholder="Add a note" />
</>
);
}class is the one mistake the compiler lets through. The app runs, but React
logs a warning in the console asking whether you meant className. Read
warnings as well as errors: a warning means the app runs, but probably not the
way you intended.
Before moving on, explain in your own words why class compiles but a missing
/> does not.
Completion is saved in this browser only. It does not measure mastery.