# Thinking in Backtick What runs on your server, what runs on the phone, and what crosses between them: server components, splices, client components, and how they compose. A Backtick screen is one file with code for two places: your server and the phone. Three rules say which code runs where: 1. **A server component runs on your server,** for every request. 2. **A client script, `` cs`…` ``, runs on the phone.** 3. **A splice, `$name`, is the only way a value crosses** from one to the other. ## One screen, both sides ```tsx title="server/Home.tsx" import { cs } from "@backtickjs/core"; import { useState } from "react" with { backtick: "client" }; import { Pressable, ScrollView, Text } from "react-native" with { backtick: "client", }; import { db, type Order, type User } from "./db.js"; // A client component: it runs on the phone, with state of its own. const ReorderButton = cs`(props: { order: Order }) => { const [added, setAdded] = $useState(false); return ( <$Pressable onPress={() => setAdded(true)}> <$Text>{added ? "Added ✓" : "Reorder " + props.order.name} ); }`; // A server component: it runs on your server, for every request. export async function Home({ user }: { user: User }) { const usual = await db.usualOrder(user.id); return cs`( <$ScrollView> <$Text>Good morning, {$user.name} <$ReorderButton order={$usual} /> )`; } ``` Read it by where each part runs: - **On your server: `Home`, and `await db.usualOrder(...)`.** It reads its data where the data lives. Queries, secrets and server-only packages stay on your server. - **Crossing as data: `$user` and `$usual`.** Each splice is a value `Home` has, written into the screen. In the script, it has its server type. - **On the phone: the script `Home` returns, and `ReorderButton`.** They draw the screen, keep state and handle taps. Your server bundles this screen for each request, and your app runs the bundle. [How it works](/docs/how-it-works/) shows what the phone receives. ## Server components ```tsx title="server/ProductScreen.tsx" import { cs } from "@backtickjs/core"; import { Text, View } from "react-native" with { backtick: "client" }; import type { Catalog } from "./catalog.js"; // A server component: a function of its props, run on your server for every // request. Its props stay on your server, so they can be anything. export async function ProductScreen({ id, catalog, }: { id: string; catalog: Catalog; }) { const product = await catalog.find(id); if (product === undefined) { return cs`<$Text>This product is gone.`; } const name = product.name; const inStock = product.stock > 0; return cs`( <$View style={{ padding: 24, gap: 8 }}> <$Text style={{ fontSize: 28, fontWeight: "bold" }}>{$name} <$Text>{$inStock ? "In stock" : "Sold out"} )`; } ``` - **Props stay on your server, so they can be anything:** a database client, a class instance like `Catalog`, a request. Only what you splice crosses. - **Decide on your server.** `ProductScreen` returns a different script for a product that's gone. The phone gets only the screen the decision led to. - **It has no state, and no hooks.** It runs once per request and returns. Calling `useState` in it is a type error: "This expression is not callable". State goes in a [client component](#client-components). ## Splices A splice carries a value from your server into a script. `$name` splices the variable `name`; `${expression}` splices any expression, as `${user.name}`. Both are evaluated on your server, when the `` cs`…` `` is. A splice can carry what can be written as data, and scripts: - Strings, numbers, bigints, booleans, `null` and `undefined` - Plain objects and arrays of those - Scripts: `` cs`…` `` values, client components and client functions - What the client provides: `$View`, imported with `with { backtick: "client" }`, `$useState` and the rest Functions and class instances can't cross, because they're code and state on your server. TypeScript stops them where you write the splice: ```tsx title="server/Signup.tsx" import { cs } from "@backtickjs/core"; import { Pressable, Text } from "react-native" with { backtick: "client" }; export async function Signup() { const opensAt = new Date(2026, 9, 6); const track = () => console.log("signup"); return cs`( // @ts-expect-error: a function on your server can't cross to the phone. <$Pressable onPress={$track}> {/* @ts-expect-error: nor can a class instance, a Date included. */} <$Text>Opens {$opensAt.toDateString()} )`; } ``` ```text Argument of type '() => void' is not assignable to parameter of type 'Spliceable'. Argument of type 'Date' is not assignable to parameter of type 'Spliceable'. ``` The fix is to cross what each one is for: the date as a string, and the handler as a client function, which runs on the phone. ```diff title="server/Signup.tsx" @@ -2,14 +2,12 @@ import { Pressable, Text } from "react-native" with { backtick: "client" }; export async function Signup() { - const opensAt = new Date(2026, 9, 6); - const track = () => console.log("signup"); + const opensAt = new Date(2026, 9, 6).toDateString(); + const track = cs`() => console.log("signup")`; return cs`( - // @ts-expect-error: a function on your server can't cross to the phone. <$Pressable onPress={$track}> - {/* @ts-expect-error: nor can a class instance, a Date included. */} - <$Text>Opens {$opensAt.toDateString()} + <$Text>Opens {$opensAt} )`; } ``` Forget the `$`, and the script looks for a global by that name: "Cannot find name 'total'". A script sees its own code and the phone's globals, never the file around it. ### A splice takes the whole value `$user.name` splices `user`, the whole object, then reads `name` on the phone. Everything in `user` crosses, including the email the screen never shows: ```tsx title="server/Whole.tsx" import { cs } from "@backtickjs/core"; import { Text } from "react-native" with { backtick: "client" }; import type { User } from "./account.js"; // `$user.name` splices `user`, the whole object, and reads its name on the // phone: the email crosses too. export async function Greeting({ user }: { user: User }) { return cs`<$Text>Hello, {$user.name}`; } ``` In the bundle the phone receives, the splice is the whole object: ```js const $thunk1 = () => ({ id: "u1", name: "Ada", email: "ada@example.com" }); ``` To send only the name, splice the expression: `${user.name}`. The same goes for anything private in an object: splice what the screen needs, not what holds it. ### Optional values Each `$offer` in a script is its own splice, so TypeScript doesn't narrow one by checking another. In `{$offer && <$Banner offer={$offer} />}`, the second `$offer` could still be `null` as far as TypeScript knows. Read the splice once into a constant, and check that: ```tsx title="server/Promo.tsx" import { cs } from "@backtickjs/core"; import { Text } from "react-native" with { backtick: "client" }; type Offer = { title: string }; // Not every user has an offer. async function offerFor(userId: string): Promise { return userId === "u-7" ? { title: "2 for 1 cold brew" } : null; } const Banner = cs`(props: { offer: Offer }) => ( <$Text>{props.offer.title} )`; export async function Promo({ userId }: { userId: string }) { const offer = await offerFor(userId); return cs`{ const offer = $offer; return offer && <$Banner offer={offer} />; }`; } ``` ## Client components A client component is a script written as a function that takes props and returns JSX, as `ReorderButton` above. It runs on the phone, where it keeps state and handles taps, and another script draws it as a tag. ```tsx title="server/Cart.tsx" import { cs } from "@backtickjs/core"; import { useState } from "react" with { backtick: "client" }; import { Text, View } from "react-native" with { backtick: "client" }; import type { ReactNode } from "react"; import { Stepper } from "./Stepper.js"; type Line = { id: string; name: string }; // A client component that draws what it's given, as `children`. const Card = cs`(props: { title: string; children: ReactNode }) => ( <$View style={{ padding: 16, gap: 8, borderRadius: 12, backgroundColor: "#f4f4f5", }} > <$Text style={{ fontWeight: "600" }}>{props.title} {props.children} )`; // State on the phone, handed to each row's stepper. const CartLines = cs`(props: { lines: Line[] }) => { const [quantities, setQuantities] = $useState>({}); return ( <$View style={{ gap: 12 }}> {props.lines.map((line) => ( <$Card key={line.id} title={line.name}> <$Stepper value={quantities[line.id] ?? 1} onChange={(value) => setQuantities({ ...quantities, [line.id]: value }) } min={1} /> ))} ); }`; export async function Cart() { const lines: Line[] = [ { id: "l1", name: "Flat white" }, { id: "l2", name: "Croissant" }, ]; return cs`<$CartLines lines={$lines} />`; } ``` - **Import it like anything else.** `Stepper` is a client component in its own file, with `value`, `onChange` and `min` props; `Cart.tsx` imports it and draws `<$Stepper />`. - **Props are typed by its parameter.** A missing or wrong prop is a type error where the tag is written. - **Inside client code, props can be anything:** `onChange` is a function, `children` is JSX. Only what crosses from your server has to be data. - **Hooks go here,** as in any React component: `$useState`, `$useEffect` and the rest. - **Lists take `key`,** as in React. - **Write it as a function, and it keeps its state.** A function script is one function for the whole bundle, so React sees the same component on every render. [Scripts in depth](/docs/scripts-in-depth/#reading-a-script) explains why an expression wouldn't. - **It's a tag in a script, never on your server.** Drawn on your server, it "does not have any construct or call signatures". ## Composing ```tsx title="server/ProductPage.tsx" import { cs } from "@backtickjs/core"; import { useState } from "react" with { backtick: "client" }; import { Pressable, ScrollView, Text, View } from "react-native" with { backtick: "client", }; import type { JSX } from "@backtickjs/jsx-react/jsx-runtime"; import { reviewsFor } from "./reviews.js"; // A client component. const LikeButton = cs`() => { const [liked, setLiked] = $useState(false); return ( <$Pressable onPress={() => setLiked(!liked)}> <$Text>{liked ? "♥ Liked" : "♡ Like"} ); }`; // A server component, drawn inside other scripts. async function Reviews({ productId }: { productId: string }) { const reviews = await reviewsFor(productId); return cs`( <$View style={{ gap: 8 }}> {$reviews.map((review) => ( <$Text key={review.id}> {review.author}: {review.text} ))} )`; } // A server component that lays out what it's given. function Section({ title, children, }: { title: string; children: JSX.Element; }) { return cs`( <$View style={{ gap: 12 }}> <$Text style={{ fontSize: 20, fontWeight: "bold" }}>{$title} {$children} )`; } export async function ProductPage({ productId }: { productId: string }) { return cs`( <$ScrollView contentContainerStyle={{ padding: 24, gap: 24 }}> <$Text style={{ fontSize: 28, fontWeight: "bold" }}> Ceramic dripper <$LikeButton /> { ${(
)} } )`; } ``` Server and client components compose in both directions, in one file: - **A client component inside a script:** `<$LikeButton />`. - **A server component inside a script:** `{${}}`, a splice of its element. It runs on your server when the bundle is built, and the phone gets only what it returns. - **A server component inside a server component:** `Section` takes `` as its children and draws them with `{$children}`. Its `children` is a `JSX.Element` when it's handed a server element, as here, and a `Client` when it's handed a script. A server component runs once per element, as React's server components do: `` written twice runs twice, and one element held in a variable and drawn in two places runs once. If two places need the same data, fetch it once and pass it down. ## Where code goes | You need | Write it as | | ------------------------------------------------------------- | --------------------------------------------------------------- | | Data from a database or an API, secrets, server-only packages | Code in a server component | | State, effects, taps, animation, device APIs | Code in a client script | | A server value on the phone | A splice: `$name`, or `${expression}` | | A piece of UI with its own props and state | A client component: `` cs`(props) => …` ``, used as `<$Name />` | | A function the phone calls | A client function: `` cs`(n: number) => …` `` | | A value every client component shares, like a context | A script: `` cs`…` ``, one value for the bundle | | A piece of UI built on your server, inside a script | A server component, spliced: `{${}}` |