Components with TSX

Pages has two template styles. ETS is HTML with code in it, and it's the one these docs use. TSX is code that returns HTML: every page, layout and partial is a function component. Pick TSX if you think in components and want typed props. Either way the page is rendered on the server and sent as plain HTML; nothing runs in the browser.

A .tsx file can be a page, a layout (+layout.tsx), an error page (+error.tsx, +404.tsx) or a partial in _private/. PocketOne compiles it the first time it's used, with esbuild built into the binary: no Node, no npm install, no build step. TypeScript types are stripped, not checked; to check them, see TypeScript. The tsx starter kit begins an app in this style.

// pb_pages/products/[id].tsx
import Price from './_private/Price'

export default function Product({ data, slot }: pages.Props) {
  slot('title', data.product.getString('name'))
  return (
    <article class="product">
      <h1>{data.product.getString('name')}</h1>
      <Price cents={data.product.getInt('price')} />
    </article>
  )
}
// pb_pages/products/_private/Price.tsx
export default ({ cents }: { cents: number }) => <span class="price">${(cents / 100).toFixed(2)}</span>

Props

The file's default export is a component: a function from props to markup. It gets:

Prop
ctx The request context.
data, params, meta The same as ctx.data, ctx.params and ctx.meta.
asset, include The same helpers EJS templates have. include(name, locals) returns HTML you can put in JSX.
slot slot(name) reads a named slot, slot(name, content) sets one, slot() is the inner content.
children In a layout, the inner content.

A partial also gets the locals it was included with, as props.

In TypeScript, these props are pages.Props (a partial's locals: pages.Props<{ label: string }>). A component you import and use as <Price /> gets only the props you pass it. See TypeScript.

Server-rendered HTML only

TSX looks like React, but it isn't: it renders straight to HTML on the server. There's no virtual DOM, no hydration, no state and no client-side React. Interactivity comes from the same places as in an ETS app: links, forms, HTMX, Datastar or a <script>.

  • Event handler props (functions) are dropped. Use plain attributes: onclick="...", hx-get, data-on:click.
  • Text and attribute values are escaped. dangerouslySetInnerHTML={{ __html: html }} inserts raw HTML.
  • class and className both work, as do for and htmlFor.
  • style takes a string or an object: { marginTop: 4 } renders margin-top:4.
  • true renders a bare attribute. false, null and undefined leave it out.
  • <html> gets <!doctype html> in front of it, since JSX can't write a doctype.

Async components

Any component can be async, including the file's default export. The render waits for every async component in the tree, and sibling components run at the same time:

// pb_pages/dashboard.tsx
async function Count({ collection }: { collection: string }) {
  const n = await $app.countRecords.async(collection)
  return <b>{n}</b>
}

export default async function Dashboard({ ctx }: pages.Props) {
  const user = ctx.auth ? await ctx.app.findRecordById.async('users', ctx.auth.id) : null
  return (
    <main>
      <p>Hi {user ? user.getString('name') : 'there'}</p>
      <p>
        <Count collection="orders" /> orders, <Count collection="users" /> users
      </p>
    </main>
  )
}

include() of a partial that awaits, and slot(name, content) with async content, work the same way.

Layouts

// pb_pages/+layout.tsx
export default function Layout({ children, slot }: pages.Props) {
  return (
    <html>
      <head>
        <title>{slot('title')}</title>
      </head>
      <body>{children}</body>
    </html>
  )
}

One style per app

Use TSX for every template in an app: pages, layouts, partials and error pages. Code files (a page's .ts, endpoints, +middleware.ts, +config.ts) are the same in both styles.

The two styles can include each other, so you can move an app from one to the other a folder at a time: an ETS page can sit inside a TSX layout, and include() returns a partial's HTML in either style. Markdown pages are the one mix you can't avoid, since .md files always run EJS tags (see Markdown).

Imports

Components are ordinary modules, so import Card from './_private/Card' resolves the same way as require. A module whose only export is export default behaves like module.exports = .... include('card') without an extension tries .ets, .ejs, .md, .jsx and .tsx, in that order.

.jsx works exactly like .tsx, without types; see File types.