Declarative routing for React Native applications with native-specific components and deep linking support
—
Native-specific hooks for custom navigation behavior, hardware integration, and deep linking support in React Native applications.
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>
);
}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 /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>
);
}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>
);
}// 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;
}useLinkPressHandler enables building custom navigation components that integrate with React RouterInstall with Tessl CLI
npx tessl i tessl/npm-react-router-native