# React Native and packages Every component, API and hook of React Native and React, spliced into scripts with their own names and types, and any other package your app ships. ```tsx title="server/Store.tsx" import { cs } from "@backtickjs/core"; import { useEffect, useRef } from "react" with { backtick: "client" }; import { Animated, Linking, Platform, Pressable, Text, useWindowDimensions, View, } from "react-native" with { backtick: "client" }; import type { ReactNode } from "react"; // Fades its children in when it appears. const FadeIn = cs`(props: { children: ReactNode }) => { const opacity = $useRef(new $Animated.Value(0)).current; $useEffect(() => { $Animated .timing(opacity, { toValue: 1, duration: 400, useNativeDriver: true }) .start(); }, []); return <$Animated.View style={{ opacity }}>{props.children}; }`; type StoreInfo = { name: string; phone: string; address: string }; const StoreCard = cs`(props: StoreInfo) => { const { width } = $useWindowDimensions(); const maps = $Platform.OS === "ios" ? { label: "Open in Maps", url: "https://maps.apple.com/?q=" } : { label: "Open in Google Maps", url: "https://www.google.com/maps/search/?api=1&query=", }; return ( <$FadeIn> <$View style={{ padding: 24, gap: 12, flexDirection: width > 600 ? "row" : "column", }} > <$Text style={{ fontSize: 24, fontWeight: "bold" }}>{props.name} <$Pressable onPress={() => $Linking.openURL("tel:" + props.phone)}> <$Text>Call {props.phone} <$Pressable onPress={() => $Linking.openURL(maps.url + encodeURIComponent(props.address)) } > <$Text>{maps.label} ); }`; export async function Store() { const store: StoreInfo = { name: "Backtick Coffee", phone: "+15550100", address: "1 Main St, Portland", }; return cs`<$StoreCard {...$store} />`; } ``` Import React Native as you would in an app, marked `with { backtick: "client" }`: the server never loads it, and each name is what the phone provides, typed as React Native types it. React's hooks are imported the same way, from `react`. In a script, splice them with `$`: - **Components are tags:** `<$View>`, `<$Pressable>`, `<$ScrollView>`. - **Members are tags too:** `<$Animated.View>` reads `View` off `Animated`. - **APIs are values:** `$Platform.OS`, `$Linking.openURL(…)`, `$StyleSheet.create(…)`. - **Hooks are called in client components,** as in any React component: `$useState`, `$useEffect`, `$useRef`, `$useWindowDimensions`. The phone runs them with the React Native your app ships, so a screen uses the same version as the rest of your app. ## The platform is the phone's `$Platform.OS` and `$useWindowDimensions()` run on the phone, so they describe the device the screen is drawn on. A server component can't know them; ask in a client component, as `StoreCard` does. ## Any package your app ships A screen imports any package your app ships the same way, and splices it like anything else: `$impactAsync()`, `<$LinearGradient>`. A package a screen uses has to be in two places: **1. The screen,** imported for the client: ```tsx title="server/Home.tsx" import { cs } from "@backtickjs/core"; import { impactAsync } from "expo-haptics" with { backtick: "client" }; import { LinearGradient } from "expo-linear-gradient" with { backtick: "client", }; import { Pressable, Text } from "react-native" with { backtick: "client" }; export async function Home() { return cs`( <$LinearGradient colors={["#7c3aed", "#0891b2"]} style={{ flex: 1, padding: 24 }} > <$Pressable onPress={() => $impactAsync()}> <$Text style={{ color: "#fff", fontSize: 24 }}>Tap to feel it )`; } ``` **2. Your app's module map,** the modules a screen may require, which the app hands to `createRuntime`: ```ts title="client/backtick.ts" import * as Haptics from "expo-haptics"; import * as LinearGradient from "expo-linear-gradient"; import * as React from "react"; import * as JSXRuntime from "react/jsx-runtime"; import * as ReactNative from "react-native"; // What a screen may require, as the app imports it: the packages it's built // with. export const moduleMap = { react: React, "react/jsx-runtime": JSXRuntime, "react-native": ReactNative, "expo-haptics": Haptics, "expo-linear-gradient": LinearGradient, }; // Their versions, as installed: what the app sends your server. Written by // `npm install`. export { default as installedPackages } from "../installed-packages.json"; ``` Install the package in your app as usual, `npx expo install expo-haptics`, so it's built into the app and its native code ships with it, and listed in `package.json`'s dependencies. `npm install` writes the versions installed to `installed-packages.json`, and the app sends your server the modules its map offers at those versions, so the server knows what each build of your app carries: ```tsx title="server/index.tsx" import { serve } from "@hono/node-server"; import { createRender } from "@backtickjs/bundler"; import { Hono } from "hono"; import { Home } from "./Home.js"; const app = new Hono(); const render = createRender({ onBundlerError: (error) => console.error(error), }); // Each screen is bundled for the modules the requesting build carries, from // its module map, at the versions it was built with. app.get("/", (context) => render(context.req.raw, )); serve({ fetch: app.fetch, port: 3000 }); ``` ## Versions are checked A client import records the version of the package your server has installed, the one its types were checked against. When a screen is bundled, the bundler checks the requesting build carries that version or a later one of the same major, and refuses a screen that build can't run, before anything reaches the phone. [Errors](/docs/errors/#cant-import--from--this-app-doesnt-have-) has the messages. ## Every build in users' hands A store build keeps the modules it was built with for as long as it's installed, while your server moves on. Each build sends the modules it carries, at their versions, and the server bundles each screen for them, so an old build gets screens it can run, with nothing to record before it ships. A server component draws an older build something it can run by asking first: `supports("expo-haptics", "^15")` answers for the build being drawn for. To draw for two majors of a package, install the second under an alias, `"acme-ui-2": "npm:acme-ui@2"`: imports from either are of `acme-ui`, each typed against its own version. A build that can't run a screen at all gets a `406` instead, whose bundle throws the bundler's `UnsupportedImportError`: the app's `exec` throws an error with that name, and a message saying why. Have the app draw its own screen asking its users to update when `error.name` is `UnsupportedImportError`: one the server bundled would need the same modules the build lacks. [`@backtickjs/core`](https://github.com/backtickjs/backtick/tree/main/packages/core#every-build-in-users-hands) and [`@backtickjs/bundler`](https://github.com/backtickjs/backtick/tree/main/packages/bundler#modules) have the details.