Skip to content

React Native quickstart

Set up a shared Nayan UI theme, configure device modules and mount a scene in a native development build.

Create a starter project

The starter is a standalone Bun workspace. Vite runs the web app, Expo runs the mobile app, and both use the landing page and theme in shared/src. Run bun run web, bun run ios, or bun run android from the project root.

The CLI requires nayan-ui 1.0.0-beta.6 on npm. Use the repository template if that release is unavailable. The generated workspace starts on the published beta.5 and can be upgraded when you need the beta.6 module APIs.

Create a project with the CLI

Terminal / configuration

Create a project with the CLI
npx nayan-ui@1.0.0-beta.6 new my-app
cd my-app
bun install --frozen-lockfile
bun run web

Use the repository template

Terminal / configuration

Use the repository template
git clone --depth 1 https://github.com/ursnj/nayan-ui.git
cd nayan-ui/templates/nayan-ui
bun install --frozen-lockfile
bun run web

Set up the native theme and modules

In the starter, mobile/src/main.tsx wraps the shared landing page in NTheme. The shared theme uses the same palette and utility classes on mobile and web. Expo’s systemTheme option aligns the native window appearance before the JavaScript screen starts.

Enable imagePicker for camera capture and set contactsPermission for device contacts. Include the module features you use if you build with a custom profile. Rebuild with bun run ios or bun run android after changing app.json.

mobile/app.json

React Native · iOS and Android

mobile/app.json
{
  "expo": {
    "userInterfaceStyle": "automatic",
    "plugins": [["nayan-ui", {
      "systemTheme": true,
      "imagePicker": true,
      "cameraPermission": "Take photos for your notes.",
      "contactsPermission": "Choose and manage contacts."
    }]]
  }
}

Write the scene

Keep the scene in plain functions so React only mounts it. useWorld calls setupScene once with a fresh world after the engine loads, and builds a new one when its deps change.

scene.ts

React and React Native · shared engine API

scene.ts
import type { Camera, World } from "nayan-ui";

export const camera: Camera = { eye: [8, 7, 10], target: [0, 1, 0], fov: 0.9 };

export function setupScene(world: World) {
  world.spawn({ mesh: "cube", position: [0, -0.5, 0], scale: [16, 1, 16], color: "#404D59", physics: "fixed" });
  world.spawn({ mesh: "sphere", position: [0, 3, 0], color: "#33A6FF", physics: { type: "dynamic", bounce: 0.65 } });
}

/** Tapping the ball bounces it. */
export function tapScene(world: World, x: number, y: number) {
  const hit = world.pick(x, y);
  if (hit) world.impulse(hit.entity, [0, 5, 0]);
}

Mount it

useWorld returns null until the world is ready and passes start-up failures to onError, such as a missing native module in Expo Go. paused stops updates and drawing but keeps the world.

GameScreen.tsx

React Native · iOS and Android

GameScreen.tsx
import { useState } from "react";
import { Button, Text, View } from "react-native";
import { GameView, useWorld } from "nayan-ui";
import { camera, setupScene, tapScene } from "./scene";

export default function GameScreen() {
  const [error, setError] = useState<string | null>(null);
  const [paused, setPaused] = useState(false);
  const world = useWorld(64, setupScene, [], (reason) => setError(reason.message));
  if (error) return <Text accessibilityRole="alert">{error}</Text>;
  if (!world) return <Text>Loading…</Text>;
  return (
    <View style={{ flex: 1 }}>
      <Button title={paused ? "Resume" : "Pause"} onPress={() => setPaused((value) => !value)} />
      <GameView source={world} camera={camera} paused={paused} style={{ flex: 1 }}
        onTap={(x, y) => tapScene(world, x, y)} onError={(reason) => setError(reason.message)} />
    </View>
  );
}

Pause when the screen loses focus

GameView already pauses while the app is in the background. Navigators keep unfocused screens mounted, so pass paused={!focused} from your navigator's focus hook.

FocusAwareGame.tsx

React Native · iOS and Android

FocusAwareGame.tsx
import { GameView, type World } from "nayan-ui";
import { camera, tapScene } from "./scene";

/** `focused` comes from your navigator, e.g. useIsFocused() from @react-navigation/native. */
export function FocusAwareGame({ world, focused }: { world: World; focused: boolean }) {
  return (
    <GameView source={world} camera={camera} paused={!focused} style={{ flex: 1 }}
      onTap={(x, y) => tapScene(world, x, y)} />
  );
}