Qat without .qx
.qx is the recommended way to write Qat components. Standard .tsx is the compatibility way: for
teams that keep a standard file type, for codebases that want unmodified tsc, Prettier and ESLint,
and as the first step of qatjs migrate. Both compile to the same output: the same runtime calls,
the same chunks, the same size and speed. A project can mix both; components import each other
across the two.
A .tsx file has no macros, no blocks and no <style>:
- reactivity is the explicit API:
const n = state(0)is a signal, readn(), writen.set(v)orn.set((prev) => next). The updater form is the compiler's: an arrow or function written in the call becomesn.update(…). A function passed by name (n.set(inc)) is stored as the value, and so a state that holds a function is set with an updater returning it,cb.set(() => handler); - control flow is components from
@qatjs/core:<If>,<For>,<Switch>,<Try>; - attributes are the same as in
.qx(class:,style:,on:,bind:,ref, spread,slot), typed by the sameJSXnamespace, so plaintscreports a wrong handler or prop type.
Everything you import from @qatjs/core for this — state, derived, mount, If, For, … — is a
compile target: the compiler turns it into exactly what the .qx form compiles to and removes the
import. Your tsconfig.json needs "jsx": "preserve" and "jsxImportSource": "@qatjs/core".
Side by side
.qx |
.tsx |
|---|---|
let n = state(0) |
const n = state(0) |
n (read) |
n() |
n = 5, n++, n += 2 |
n.set(5), n.set(n() + 1), n.set((v) => v + 2) |
user.name = 'x' (deep) |
user().name = 'x' |
let rows = shallow<Row[]>([]) |
const rows = shallow<Row[]>([]) |
let total = derived(price * qty) |
const total = derived(() => price() * qty()) |
effect(() => log(n)) |
effect(() => log(n())) |
watch(n, (now, before) => …) |
watch(() => n(), (now, before) => …) |
mount(fn), unmount(fn), idle(fn) |
the same, imported from @qatjs/core |
let r = load(fn), r.data |
const r = load(fn), r.data() |
let emit = emits<E>() |
const emit = emits<E>(); declare the events in props with Events<E> |
expose({ focus }) |
the same; declare ref?: Ref<Handle> in props |
track(ref(n)) (pass the signal) |
track(n) (a signal is already a value) |
{if (a)} A {else if (b)} B {else} C {/if} |
<If when={a} fallback={<If when={b} fallback={C}>B</If>}>A</If> |
{for (const r of rows)} <li key={r.id}>{r.label}</li> {/for} |
<For each={rows} key={(r) => r.id}>{(r) => <li>{r().label}</li>}</For> |
{for (const [i, x] of entries(xs))} |
<For each={xs}>{(x, i) => …}</For> (i() is the index) |
{switch (m)} {case 'a'} A {default} D {/switch} |
<Switch on={m}><Case value="a">A</Case><Default>D</Default></Switch> |
{try} … {catch (err, retry)} … {/try} |
<Try fallback={(err, retry) => …}>…</Try> (err() is the error) |
on:submit|prevent={fn} |
on:submit={withModifiers(fn, ['prevent'])} (['debounce:300'] too; the event layer's other .tsx spellings: events) |
<style>…</style> |
import './card.css' with { scope: 'component' } |
return <h1/> <p/> |
return [<h1/>, <p/>] |
<If>, <For>, <Switch> and <Try> can also be the whole result of a component
(return <If when={user()} fallback={<Login />}><Profile /></If>) — the reactive form of React's
early return — or one of several results in an array; anywhere else outside JSX children they are
an error (QX-CONTROL-ROOT).
Two rules follow from "a reactive read is always a call":
- A keyed
<For>item is an accessor. Withkey, a row is matched by its key and its item can be replaced in place, so read it asitem(). Withoutkeythe item is the plain value. - No automatic unwrapping.
.qxreads an imported signal or an injected value throughu()for you; in.tsxyou call what is a signal and use what is a value.
A component
import { state, derived, mount, If, For } from '@qatjs/core'
export default function UserCard({ user }: { user: { name: string; bio: string; hidden?: boolean; tags: string[] } }) {
const likes = state(0)
const label = derived(() => `${likes()} likes`)
mount(() => console.log('mounted'))
return (
<article class="card">
<h2>{user.name}</h2>
<If when={!user.hidden}><p>{user.bio}</p></If>
<ul><For each={user.tags} key={(t) => t}>{(t) => <li>{t()}</li>}</For></ul>
<button on:click={() => likes.set((n) => n + 1)}>{label()}</button>
</article>
)
}likes statelabel derivedThe same component in .qx:
export default function UserCard({ user }) {
let likes = state(0)
let label = derived(`${likes} likes`)
mount(() => console.log('mounted'))
return (
<article class="card">
<h2>{user.name}</h2>
{if (!user.hidden)}<p>{user.bio}</p>{/if}
<ul>{for (const t of user.tags)}<li key={t}>{t}</li>{/for}</ul>
<button on:click={() => likes++}>{label}</button>
</article>
)
}likes statelabel derivedEvents, bind: and ref on a .tsx component
A .qx component's events and handle are inferred from emits<E>() and expose(). A .tsx
component declares them in its props, with two helper types:
import { state, emits, expose, type Events, type Ref } from '@qatjs/core'
export interface CounterHandle { reset(): void }
export default function Counter(props: { value?: number; ref?: Ref<CounterHandle> } & Events<{ 'update:value': number }>) {
const emit = emits<{ 'update:value': number }>()
const local = state(0)
expose({ reset: () => local.set(0) })
return <button on:click={() => emit('update:value', (props.value ?? 0) + 1)}>{props.value} {local()}</button>
}local stateEvents<{ 'update:value': number }> gives the component an on:update:value prop and lets a parent
write bind:value={n} (with n a signal in .tsx, a state variable in .qx).