Bitpath

Lesson 1 · Unit 01

JSX

Markup inside TypeScript: braces for expressions, attributes, one root, and what a tag really is.

13 exercisesabout 3 min

What you practise

  • Markup inside JavaScript, {} for expressions, attributes and className, 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?

TypeScript
const name = 'Ada';
const out = <p>Hi {name}!</p>;

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.

TypeScript
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.

TypeScript
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.

TypeScript
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.

One email when Bitpath is ready to try. Nothing else. Privacy