这些库涵盖了 React 在 Web 和移动端(React Native)的核心导航与交互需求。react-router 和 react-router-dom 专为 Web 环境设计,处理浏览器历史与 URL 路由;react-navigation 和 react-native-navigation 是 React Native 的两大导航解决方案,分别侧重 JavaScript 配置与原生性能;react-native-gesture-handler 则是底层的原生手势识别库,常作为其他库的依赖或用于复杂交互场景。理解它们的适用平台与架构差异是做出正确技术选型的关键。
在 React 开发中,导航(Navigation)与路由(Routing)是实现页面跳转与状态管理的核心。然而,Web 端与移动端(React Native)的运行环境截然不同,导致解决方案无法通用。本文将深入对比这五个关键库,帮助你在不同场景下做出准确的技术决策。
在 Web 开发中,react-router 与 react-router-dom 的关系常被误解。它们不是竞争关系,而是核心与实现的关系。
react-router 提供了路由的核心逻辑,不依赖任何特定平台。
// react-router: 核心用法(通常配合自定义 history)
import { Router, Routes, Route } from 'react-router';
import { createBrowserHistory } from 'history';
const history = createBrowserHistory();
function App() {
return (
<Router location={history.location} navigator={history}>
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</Router>
);
}
react-router-dom 是专为 Web 浏览器设计的实现包。
BrowserRouter,自动处理 HTML5 History API。Link、NavLink 等针对 DOM 优化的组件。// react-router-dom: 标准 Web 用法
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<nav>
<Link to="/">Home</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</BrowserRouter>
);
}
在移动端,react-navigation 与 react-native-navigation 是两大主流方案,它们的架构理念完全不同。
react-navigation 采用 JavaScript 驱动的配置方式。
// react-navigation: JS 驱动配置
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
const Stack = createNativeStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
react-native-navigation 采用完全原生的导航控制器。
// react-native-navigation: 原生控制
import { Navigation } from 'react-native-navigation';
Navigation.setRoot({
root: {
stack: {
children: [
{
component: {
name: 'HomeScreen',
options: {
topBar: {
title: {
text: 'Home'
}
}
}
}
}
]
}
}
});
react-native-gesture-handler 是 React Native 中处理手势的基础设施。
react-navigation v6+ 强制依赖此库以实现流畅的侧滑返回。// react-native-gesture-handler: 原生手势包装
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { TapGestureHandler } from 'react-native-gesture-handler';
function App() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<TapGestureHandler onActivated={() => console.log('Tapped')}>
<View style={{ flex: 1, backgroundColor: 'white' }} />
</TapGestureHandler>
</GestureHandlerRootView>
);
}
选择导航库不仅仅是选 API,更是选择架构模型。
react-navigation 的导航状态在 JS 线程。
// react-navigation: 动态路由配置
<Stack.Screen
name="Profile"
component={ProfileScreen}
options={({ route }) => ({ title: route.params.userName })}
/>
react-native-navigation 的导航状态在原生线程。
// react-native-navigation: 原生传参
Navigation.push('componentId', {
component: {
name: 'Profile',
passProps: {
userName: 'Alice'
}
}
});
react-router-dom 和 react-navigation 偏好约定优于配置。
// react-router-dom: 简单声明
<Route path="/user/:id" element={<User />} />
react-native-navigation 需要原生层配合。
AppDelegate 和 Android MainActivity 中注册组件。// react-native-navigation: 原生注册 (iOS 示例)
[RCTRootView registerComponent:@"HomeScreen" lookupClass:^Class {
return [HomeScreen class];
}];
在 React Native 中,导航往往依赖手势库。
react-navigation 内部集成 react-native-gesture-handler 实现侧滑返回。react-native-navigation,手势需额外配置。react-native-gesture-handler 的根视图包装会导致手势失效。// 错误:缺少根视图包装,手势可能不响应
export default function App() {
return <NavigationContainer>...</NavigationContainer>;
}
// 正确:包裹根视图
export default function App() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<NavigationContainer>...</NavigationContainer>
</GestureHandlerRootView>
);
}
| 特性 | react-router-dom | react-navigation | react-native-navigation |
|---|---|---|---|
| 平台 | Web 浏览器 | React Native | React Native |
| 控制层 | JS (DOM 绑定) | JS (原生渲染) | 完全原生 |
| 配置难度 | 低 | 中 | 高 (需改原生代码) |
| 性能表现 | 取决于 DOM 复杂度 | 优秀 (多数场景) | 极致 (原生转场) |
| 手势依赖 | 无 | 依赖 gesture-handler | 可选 |
| 适用场景 | 所有 Web 应用 | 大多数 RN 应用 | 重原生体验 RN 应用 |
Web 项目:无需犹豫,直接使用 react-router-dom。除非你在编写底层路由库,否则不要单独安装 react-router。
React Native 项目:
react-navigation。它平衡了性能与开发效率,社区支持最好,且能解决 95% 的需求。react-native-navigation。react-native-gesture-handler 并按文档正确包裹根组件,这是保证移动端交互流畅的基础。核心原则:不要为了微小的性能提升而牺牲开发效率,除非你的应用瓶颈确实在导航转场上。对于大多数业务驱动的应用,react-navigation 配合 gesture-handler 是最稳健的组合。
当你在 React Native 中需要处理复杂的手势交互(如滑动删除、多指操作)或直接使用 react-navigation v6+ 时,必须选择此库。它提供了原生层的手势识别能力,避免了 JavaScript 线程阻塞导致的交互延迟。如果你的项目仅涉及简单点击且未使用依赖它的导航库,可能不需要直接引入。
如果你追求极致的原生导航性能、需要完全原生的转场动画,或者应用重度依赖原生 tab 栏与侧边栏,应选择此库。它由 Wix 维护,采用完全原生的导航栈,但配置较复杂,需要修改原生 iOS 和 Android 项目文件。适合对原生体验要求极高且愿意投入原生维护成本的团队。
对于大多数 React Native 项目,这是首选方案。它基于 JavaScript 配置,易于定制,社区生态丰富,且支持多种导航模式(栈、标签、抽屉)。虽然底层依赖原生组件,但控制逻辑在 JS 层,开发效率更高。适合希望快速迭代、跨平台一致性高且不想深入原生配置的团队。
仅在需要自定义路由逻辑或在非 DOM 环境(如 React Native 中手动实现路由)时使用核心包。在标准 Web 开发中,你通常不需要直接安装它,因为 react-router-dom 已经包含了所需功能。适合库作者或需要深度控制路由历史对象的高级场景。
所有标准 React Web 应用都应选择此包。它提供了 BrowserRouter、Link 等针对浏览器环境优化的组件,处理了 HTML5 历史 API 与 DOM 渲染的绑定。除非你在构建非 Web 平台的特殊路由方案,否则这是 Web 路由的唯一标准选择。
React Native Gesture Handler provides native-driven gesture management APIs for building best possible touch-based experiences in React Native.
With this library gestures are no longer controlled by the JS responder system, but instead are recognized and tracked in the UI thread. It makes touch interactions and gesture tracking not only smooth, but also dependable and deterministic.
Check getting started section of our docs for the detailed installation instructions.
Check out our dedicated documentation page for info about this library, API reference and more: https://docs.swmansion.com/react-native-gesture-handler/docs/
If you want to play with the API but don't feel like trying it on a real app, you can run the example project. Clone the repo, go to the example folder and run:
yarn install
Run yarn start to start the metro bundler
Run yarn android or yarn ios (depending on which platform you want to run the example app on).
You will need to have an Android or iOS device or emulator connected.
react-native-gesture-handler supports the three latest minor releases of react-native.
Check out our compatibility table in documentation.
[!IMPORTANT] Minimal supported
react-nativeversion for Gesture Handler 3 is0.82
| version | react-native version |
|---|---|
| 2.32.0+ | 0.84.0+ |
| 2.28.0+ | 0.79.0+ |
| 2.26.0+ | 0.78.0+ |
| 2.25.0+ | 0.76.0+ |
| 2.24.0+ | 0.75.0+ |
| 2.21.0+ | 0.74.0+ |
| 2.18.0+ | 0.73.0+ |
| 2.16.0+ | 0.68.0+ |
| 2.14.0+ | 0.67.0+ |
| 2.10.0+ | 0.64.0+ |
| 2.0.0+ | 0.63.0+ |
It may be possible to use newer versions of react-native-gesture-handler on React Native with version <= 0.59 by reverse Jetifying. Read more on that here https://github.com/mikehardy/jetifier#to-reverse-jetify--convert-node_modules-dependencies-to-support-libraries
Gesture handler library is licensed under The MIT License.
Join the Software Mansion Community Discord to chat about Gesture Handler or other Software Mansion libraries.
Since 2012 Software Mansion is a software agency with experience in building web and mobile apps. We are Core React Native Contributors and experts in dealing with all kinds of React Native issues. We can help you build your next dream product – Hire us.