JSX essentials

New to Jeasx? This tour walks you through everything you need to know about jsx-async-runtime, the engine that turns your JSX into HTML. If you know JSX from React, most of it will feel familiar. The differences are small, but important.

Each step shows a short piece of code, the rendered result and the HTML the server sends to the browser.

1 Components are functions

A component is a plain function that receives its attributes as an object (the props) and returns JSX. Its name starts with an uppercase letter, so JSX can tell it apart from HTML tags like <p>.

Code
function Greeting({ name }) {
  return <p>Hello, {name}!</p>;
}

<Greeting name="Ada" />
Result

Hello, Ada!

HTML
<p>Hello, Ada!</p>

There are no hooks, no state and no virtual DOM: a component runs once and returns markup.

2 Children and fragments

Everything between the opening and closing tag of a component arrives as the children prop. Use a fragment <>…</> to return several elements without a wrapper.

Code
function Box({ title, children }) {
  return (
    <fieldset>
      <legend>{title}</legend>
      {children}
    </fieldset>
  );
}

<Box title="Fragments">
  <>
    <p>First</p>
    <p>Second</p>
  </>
</Box>
Result
Fragments

First

Second

HTML
<fieldset>
  <legend>Fragments</legend>
  <p>First</p>
  <p>Second</p>
</fieldset>

3 HTML attribute names

Unlike React, attributes keep their HTML names: class, for and onclick instead of className, htmlFor and onClick. Styles are plain strings, and true renders a boolean attribute, while false, null and undefined leave it out.

Code
<label for="email" class="label">Email</label>
<input
  id="email"
  type="email"
  style="max-width: 16rem"
  required={true}
  disabled={false}
/>
Result
HTML
<label for="email" class="label">Email</label>
<input
  id="email"
  type="email"
  style="max-width: 16rem"
  required
/>

4 Conditions and lists

JSX has no special template syntax: use &&, the ternary operator and .map() like in any JavaScript. Arrays are rendered item by item, and null, undefined and booleans render nothing.

Code
function Inbox({ messages }) {
  return (
    <div>
      {messages.length === 0 && <p>No messages.</p>}
      {messages.length > 0 && <p>{messages.length} new messages:</p>}
      <ul>
        {messages.map((message) => (
          <li>{message}</li>
        ))}
      </ul>
    </div>
  );
}

<Inbox messages={["Hi!", "Lunch?"]} />
Result

2 new messages:

  • Hi!
  • Lunch?
HTML
<div>
  <p>2 new messages:</p>
  <ul>
    <li>Hi!</li>
    <li>Lunch?</li>
  </ul>
</div>

No key props needed: the server just renders the list once.

5 Escaping and raw HTML

All text and attribute values are escaped, so user input can never inject markup. When you really need raw HTML (e.g. the doctype or trusted content from a CMS), wrap it in html().

Code
import { html } from "jsx-async-runtime";

const input = "<b>bold</b>";

<p>Escaped: {input}</p>
<p>Raw: {html(input)}</p>
Result

Escaped: <b>bold</b>

Raw: bold

HTML
<p>Escaped: &lt;b&gt;bold&lt;/b&gt;</p>
<p>
  Raw:
  <b>bold</b>
</p>

Never pass untrusted input to html(). Inline <script> and <style> content is escaped too, so wrap it in html() as well.

6 Async components

Here comes the twist: components can be async. Fetch data right where you need it, with no loaders, effects or suspense boundaries. The runtime waits for the component and renders the result in place.

Code
async function Weather({ city }) {
  const { temperature } = await loadWeather(city);
  return (
    <p>
      {city}: {temperature} °C
    </p>
  );
}

<Weather city="Berlin" />
Result

Berlin: 12 °C

HTML
<p>Berlin: 12 °C</p>

Promises work anywhere, as children and as attribute values, e.g. <img src={loadAvatarUrl()} />. If a component throws, the whole render fails; wrap risky parts in an error boundary to show a fallback instead (see Error boundaries).

7 Loading in parallel

Async components run one after another, in the order they appear. To load data at the same time, start the requests first and pass the promises as props. Each component awaits its own promise, the requests run in parallel, and the output keeps the order of your JSX.

Code
function Forecast() {
  // Both requests start right away and run at the same time.
  const berlin = loadWeather("Berlin");
  const paris = loadWeather("Paris");
  return (
    <ul>
      <City weather={berlin} />
      <City weather={paris} />
    </ul>
  );
}

async function City({ weather }) {
  const { city, temperature } = await weather;
  return <li>{city}: {temperature} °C</li>;
}
Result
  • Berlin: 12 °C
  • Paris: 15 °C
HTML
<ul>
  <li>Berlin: 12 °C</li>
  <li>Paris: 15 °C</li>
</ul>

8 Sharing data via 'this'

All components of one request share a context object as this. Store a value once and read it deep down the tree, without passing it through every component in between. In Jeasx, this also holds the request and reply.

Code
function Page() {
  this.user = "Ada";
  return <header><Nav /></header>;
}

function Nav() {
  return <nav><UserMenu /></nav>;
}

function UserMenu() {
  return <span>Signed in as {this.user}</span>;
}
Result
HTML
<header>
  <nav><span>Signed in as Ada</span></nav>
</header>

Components using this must be regular functions, not arrow functions. Each request gets a fresh context, so nothing leaks between visitors.

9 Rendering to a string

Jeasx renders your routes for you, but you can call the runtime yourself, e.g. in tests, for emails or in scripts. jsxToString returns the HTML as a string, or a promise if rendering had to wait. With await, both cases work the same.

Code
import { jsxToString } from "jsx-async-runtime";

const markup = await jsxToString(<Weather city="Paris" />);

<pre>{markup}</pre>
Result
<p>Paris: 15 °C</p>
HTML
<pre>&lt;p&gt;Paris: 15 °C&lt;/p&gt;</pre>

The result is plain text, so <pre> shows the escaped tags. Wrap it in html() only to insert trusted, pre-rendered markup, e.g. from a cache. To send markup while slow components are still loading, use jsxToStream instead (see Streaming HTML).

Where to go next

You now know the essentials. Dive deeper with What JSX renders, 'this' as context, Streaming HTML or Unit tests, or read the jsx-async-runtime documentation.