CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-router-native

Declarative routing for React Native applications with native-specific components and deep linking support

Pending
Overview
Eval results
Files

navigation-hooks.mddocs/

Navigation Hooks

Native-specific hooks for custom navigation behavior, hardware integration, and deep linking support in React Native applications.

Capabilities

useLinkPressHandler

Handles the press behavior for router <Link> components. Useful for creating custom navigation components with the same press behavior as the built-in Link.

/**
 * Handles press behavior for router Link components
 * Creates a press handler function for custom Link implementations
 * @param to - Navigation target (string or path object)
 * @param options - Navigation options
 * @returns Press handler function for touch events
 */
function useLinkPressHandler(
  to: To,
  options?: {
    replace?: boolean;
    state?: any;
    relative?: RelativeRoutingType;
  }
): (event: GestureResponderEvent) => void;

Usage Examples:

import { useLinkPressHandler } from "react-router-native";
import { TouchableOpacity, Text } from "react-native";

// Custom link component using useLinkPressHandler
function CustomLink({ to, children, style }) {
  const handlePress = useLinkPressHandler(to);
  
  return (
    <TouchableOpacity onPress={handlePress} style={style}>
      {children}
    </TouchableOpacity>
  );
}

// Custom link with replace navigation
function ReplaceLink({ to, children }) {
  const handlePress = useLinkPressHandler(to, { replace: true });
  
  return (
    <TouchableOpacity onPress={handlePress}>
      <Text>{children}</Text>
    </TouchableOpacity>
  );
}

// Custom link with state
function StatefulLink({ to, state, children }) {
  const handlePress = useLinkPressHandler(to, { state });
  
  return (
    <TouchableOpacity onPress={handlePress}>
      <Text>{children}</Text>
    </TouchableOpacity>
  );
}

// Custom button that navigates
function NavigateButton({ destination, userData }) {
  const handlePress = useLinkPressHandler(destination, {
    state: { user: userData },
    replace: false
  });
  
  return (
    <TouchableOpacity
      onPress={handlePress}
      style={{ padding: 12, backgroundColor: "#007AFF", borderRadius: 8 }}
    >
      <Text style={{ color: "white", textAlign: "center" }}>
        Continue
      </Text>
    </TouchableOpacity>
  );
}

useDeepLinking

Enables deep linking support for both initial app launch and subsequent incoming links. Automatically handles URL schemes and navigates to appropriate routes.

/**
 * Enables deep linking support for initial app launch and incoming links
 * Listens for URL events and navigates to matching routes
 * Must be called within a component inside the router
 */
function useDeepLinking(): void;

Usage Examples:

import { useDeepLinking } from "react-router-native";
import { View, Text } from "react-native";

// Enable deep linking in your app
function App() {
  return (
    <NativeRouter>
      <DeepLinkHandler />
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/product/:id" element={<Product />} />
        <Route path="/user/:userId" element={<Profile />} />
      </Routes>
    </NativeRouter>
  );
}

function DeepLinkHandler() {
  useDeepLinking();
  return null; // This component only handles deep linking
}

// Alternative: Enable in a main screen component
function MainScreen() {
  useDeepLinking();
  
  return (
    <View>
      <Text>Welcome to the app!</Text>
      {/* Other UI elements */}
    </View>
  );
}

// URL scheme examples that would work:
// myapp://product/123 -> navigates to /product/123
// myapp://user/456 -> navigates to /user/456
// myapp:// -> navigates to /

useHardwareBackButton

Enables support for the hardware back button on Android devices. Currently a placeholder implementation that can be extended for custom back button handling.

/**
 * Enables support for hardware back button on Android
 * Currently a placeholder implementation
 * Sets up event listeners for hardware back button presses
 */
function useHardwareBackButton(): void;

Usage Examples:

import { useHardwareBackButton } from "react-router-native";
import { View, Text } from "react-native";

// Enable hardware back button handling
function App() {
  return (
    <NativeRouter>
      <BackButtonHandler />
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/settings" element={<Settings />} />
      </Routes>
    </NativeRouter>
  );
}

function BackButtonHandler() {
  useHardwareBackButton();
  return null;
}

// Or enable in a main component
function MainLayout() {
  useHardwareBackButton();
  
  return (
    <View>
      <Text>Main Layout</Text>
      {/* Navigation and content */}
    </View>
  );
}

useAndroidBackButton

Alias for useHardwareBackButton. Provides the same functionality with a more explicit name for Android-specific back button handling.

/**
 * Alias for useHardwareBackButton
 * Enables support for hardware back button on Android
 */
function useAndroidBackButton(): void;

Usage Examples:

import { useAndroidBackButton } from "react-router-native";

// Same usage as useHardwareBackButton
function AndroidApp() {
  useAndroidBackButton();
  
  return (
    <View>
      <Text>Android App with Back Button Support</Text>
    </View>
  );
}

Types

// Navigation target type
type To = string | Partial<Path>;

// Relative routing behavior
type RelativeRoutingType = "route" | "path";

// React Native gesture event
interface GestureResponderEvent {
  nativeEvent: any;
  currentTarget: any;
  target: any;
  preventDefault(): void;
  // ... other event properties
}

// Path object structure
interface Path {
  pathname?: string;
  search?: string;
  hash?: string;
}

Integration Notes

  • Deep Linking Setup: Requires proper URL scheme configuration in your React Native project
  • Android Back Button: Currently a placeholder implementation - can be extended for custom behavior
  • Custom Components: useLinkPressHandler enables building custom navigation components that integrate with React Router
  • Event Handling: All hooks work within the React Router context and require components to be inside a Router
  • Platform Support: Deep linking works on both iOS and Android, back button hooks are Android-specific

Install with Tessl CLI

npx tessl i tessl/npm-react-router-native

docs

index.md

native-routing.md

navigation-hooks.md

react-router-components.md

react-router-hooks.md

react-router-utilities.md

search-parameters.md

tile.json