Teardown

Getting Started

Set up type-safe navigation in your React Native app.

This guide walks through setting up @teardown/navigation in a React Native project.

Prerequisites

  • React Native 0.72+
  • React Navigation 7+
  • Node.js 18+

Installation

Core Dependencies

# Navigation packages
bun add @teardown/navigation @react-navigation/native @react-navigation/native-stack

# Required React Navigation dependencies
bun add react-native-screens react-native-safe-area-context

Metro Plugin

The Metro plugin scans your route files and generates TypeScript types:

bun add -D @teardown/navigation-metro

Optional Navigators

Install based on your needs:

# Tab navigation
bun add @react-navigation/bottom-tabs

# Drawer navigation
bun add @react-navigation/drawer react-native-gesture-handler react-native-reanimated

Project Setup

1. Metro Configuration

Create or update metro.config.js:

const { getDefaultConfig } = require("@react-native/metro-config");
const { withTeardownNavigation } = require("@teardown/navigation-metro");

const config = getDefaultConfig(__dirname);

module.exports = withTeardownNavigation(config, {
  // Optional: Override defaults
  routesDir: "./src/_routes",
  generatedDir: "./.teardown",
  verbose: false,
});

2. Create Routes Directory

mkdir -p src/_routes

3. Create Root Layout

The root _layout.tsx defines your app's navigator structure:

// src/_routes/_layout.tsx
import { defineLayout } from "@teardown/navigation";

export default defineLayout({
  type: "stack",
  screenOptions: {
    headerShown: true,
    animation: "slide_from_right",
  },
});

4. Create Home Screen

// src/_routes/index.tsx
import { View, Text } from "react-native";
import { defineScreen } from "@teardown/navigation";

function HomeScreen() {
  return (
    <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
      <Text>Welcome Home</Text>
    </View>
  );
}

export default defineScreen({
  component: HomeScreen,
  options: {
    title: "Home",
  },
});

5. Create Router Component

// src/app/index.tsx
import { createTeardownRouter } from "@teardown/navigation";
import { routeTree } from "../.teardown/routeTree.generated";
import { linking } from "../.teardown/linking.generated";

export const Router = createTeardownRouter({
  routeTree,
  linking,
  initialRouteName: "index",
});

6. Use Router in App Entry

// App.tsx
import { Router } from "./src/app";

export default function App() {
  return <Router />;
}

Generated Files

When you run the Metro bundler, the plugin generates files in .teardown/:

FilePurpose
routes.generated.tsRoute parameter types
routeTree.generated.tsNavigator tree structure
linking.generated.tsDeep linking configuration
register.d.tsType augmentation for hooks
manifest.jsonRoute metadata

Add .teardown/ to your .gitignore:

# Generated navigation files
.teardown/

Configuration Options

The withTeardownNavigation function accepts these options:

OptionTypeDefaultDescription
routesDirstring"./src/_routes"Path to routes directory
generatedDirstring"./.teardown"Output path for generated files
prefixesstring[][]Deep link URL prefixes
verbosebooleanfalseEnable verbose logging
autoTemplatebooleantrueAuto-populate new route files
usePollingbooleanfalseUse polling for file watching

Options can also be set in teardown.config.ts:

// teardown.config.ts
export default {
  name: "MyApp",
  slug: "myapp",
  navigation: {
    routesDir: "./src/_routes",
    generatedDir: "./.teardown",
    verbose: false,
  },
};

Verifying Setup

  1. Start the Metro bundler:
bun run dev
# or
npx react-native start
  1. Check that .teardown/ is generated with type files

  2. Navigate in your app - you should see the home screen

Next Steps