function Greeting({ name }) {
return <p>Hello, {name}!</p>;
}
<Greeting name="Ada" />Hello, Ada!
<p>Hello, Ada!</p>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.
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>.
function Greeting({ name }) {
return <p>Hello, {name}!</p>;
}
<Greeting name="Ada" />Hello, Ada!
<p>Hello, Ada!</p>There are no hooks, no state and no virtual DOM: a component runs once and returns markup.
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.
function Box({ title, children }) {
return (
<fieldset>
<legend>{title}</legend>
{children}
</fieldset>
);
}
<Box title="Fragments">
<>
<p>First</p>
<p>Second</p>
</>
</Box>
<fieldset>
<legend>Fragments</legend>
<p>First</p>
<p>Second</p>
</fieldset>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.
<label for="email" class="label">Email</label>
<input
id="email"
type="email"
style="max-width: 16rem"
required={true}
disabled={false}
/>
<label for="email" class="label">Email</label>
<input
id="email"
type="email"
style="max-width: 16rem"
required
/>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.
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?"]} />2 new messages:
<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.
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().
import { html } from "jsx-async-runtime";
const input = "<b>bold</b>";
<p>Escaped: {input}</p>
<p>Raw: {html(input)}</p>Escaped: <b>bold</b>
Raw: bold
<p>Escaped: <b>bold</b></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.
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.
async function Weather({ city }) {
const { temperature } = await loadWeather(city);
return (
<p>
{city}: {temperature} °C
</p>
);
}
<Weather city="Berlin" />Berlin: 12 °C
<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).
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.
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>;
}
<ul>
<li>Berlin: 12 °C</li>
<li>Paris: 15 °C</li>
</ul>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.
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>;
}
<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.
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.
import { jsxToString } from "jsx-async-runtime";
const markup = await jsxToString(<Weather city="Paris" />);
<pre>{markup}</pre><p>Paris: 15 °C</p>
<pre><p>Paris: 15 °C</p></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).
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.