Lesson 1 · Unit 01
JSX
Markup inside TypeScript: braces for expressions, attributes, one root, and what a tag really is.
What you practise
- Markup inside JavaScript,
{}for expressions, attributes andclassName, one root, what JSX compiles to in outline
Try a question from this lesson
Pick one
This is JSX: markup written inside TypeScript. What HTML does out become?
const name = 'Ada';
const out = <p>Hi {name}!</p>;Correct answer
<p>Hi Ada!</p>
Why
Curly braces are a window back into JavaScript: {name} is replaced by the value of the variable. Outside the braces, text is just text, which is why Hi and ! come through unchanged.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Markup that is JavaScript
JSX looks like HTML but lives in a .tsx file next to your logic. A tag is a value: store it in a variable, return it from a function, pass it around. {} switches from markup back to JavaScript for one expression, anywhere text or an attribute value could go.
const n = 3;
const a = <b>{n}</b>;
const b = <b>{n * 2} left</b>;
const c = <b>{n > 2 ? 'many' : 'few'}</b>;Concept
One root
A JSX expression has one outermost element, because it becomes one value and a function returns one value. When you do not want an extra <div> on the page, wrap the siblings in an empty tag, <> and </>, called a Fragment. It groups them and adds nothing to the HTML.
const page = (
<>
<h1>Inbox</h1>
<p>3 new</p>
</>
);Concept
What a tag becomes
The build step rewrites each tag as a function call that returns a small plain object: the tag type and its props. Nothing touches the page yet. React reads these objects later and creates the real DOM nodes. The object is called a React element, and it is cheap: a description, not a thing on screen.
const el = <b id="x">hi</b>;
// compiles to roughly:
// jsx('b', { id: 'x', children: 'hi' })
// which returns roughly:
// { type: 'b', props: { id: 'x',
// children: 'hi' } }9 more exercises in the app
- 2× type the answer
- 1× true or false
- 3× multiple choice
- 1× fill the blanks
- 1× match the pairs
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.