https://reactnavigation.org/ ——源于React Native社区对基于Javascript的可扩展且使用简单的导航解决方案的需求 。
和h5用a标签来跳转不太一样的是,rn必须依赖导航器跳转。导航器也可以看成是一个普通的React组件,你可以通过导航器来定义你的APP中的导航结构。导航还可以渲染通用元素,例如可以配置的标题栏和选项卡栏。
react-natvigation自开源以来。在短短不到3个月的时间,github上星数已达4000+。它是Fb推荐使⽤库, 并且在React Native当前最新版本0.44中将Navigator删除。react-navigation据称有原生般的性能体验效果。可能会成为未来React Native导航组件的主流军 。
可以粗略地理解,navigation就是rn版的router。
注:从19年7月到现在不到两个月,navigation有了大的更新。看官网文档也未必有用。经过笔者一天的踩坑,想要成功运行请严格执行以下操作。其它不能保证。 请确保项目绝对路径无中文。
安装(0.60-)时除了本体,还需要一个手势库:
yarn add react-navigation@3.11.1
yarn react-native-gesture-handler@1.3.0
接着需要手动link它(关联原生):
react-native link react-native-gesture-handler
此时ios已经可以跑了。但安卓配置还没完。把以下代码添加到 MainActivity.java
package com.rn;
import com.facebook.react.ReactActivity;
// add
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
// end add
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "rn";
}
// add
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
@Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}
// end add
}
到了这一步,就可以启动应用了。
在开始学习导航器之前,我们需要了了解两个和导航有关的概念:
在react-navigation中有以下类型的导航器:
你可以通过以上几种导航器来创建你的APP,可以是其中一个,也可以多个组合,这个可以根据具体的应 ⽤场景并结合每⼀个导航器器的特性进⾏选择。
创建一个src目录,如图:
在page下写几个页面(HomePage,MyPage,HotPage):
import React,{Component} from 'react';
import {View,Text,StyleSheet} from 'react-native';
export default class HomePage extends Component{
render(){
return (
<View style={styles.container}>
<Text style={styles.text}>HomePage</Text>
</View>
)
}
}
const styles=StyleSheet.create({
container:{
flex:1,
justifyContent:'center',
alignItems:'center',
backgroundColor:'#f5f5f5'
},
text:{
fontSize:26
}
})
在Navigator下新建AppNavigator.js:
import {createAppContainer} from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import HomePage from '../Pages/HomePage';
import MyPage from '../Pages/MyPage';
import HotPage from '../Pages/HotPage';
const AppNavigator=createStackNavigator({
Home:{
screen:HomePage
},
My:{
screen:MyPage
},
Hot:{
screen:HotPage
}
});
export default createAppContainer(AppNavigator);
跑一下程序,看到:
就算运行成功了。
这里使用堆栈导航。
createStackNavigator 提供APP屏幕之间切换的能⼒,它是以栈的形式还管理屏幕之间的切换,新切换到的屏幕会放在栈的顶部。
createStackNavigator API
createStackNavigator(RouteConfigs, StackNavigatorConfig):
在HomePage引入button,给它加个 onPress
事件:
<View style={styles.container}>
<Button
title={'跳转到我的页面'}
onPress={()=>{
console.log(this.props)
}}
></Button>
<Text style={styles.text}>HotPage</Text>
</View>
然后页面上出现一个button,点击一下,控制台弹出了以下内容:
通过navigation。封装了一系列方法。
注意:一个navigation有可能没有navigate、setParams以及goBack,只有state与dispatch,所以在使用navigate时要进⾏判断,如果没有navigate可以使⽤navigation去dispatch一个新的action。
接收两个参数,第一个是定义好的路由名,第二个是页面参数。
<Button
title={'跳转到我的页面'}
onPress={()=>{
console.log(this.props)
this.props.navigation.navigate('My',{
title:'我的'
})
}}
></Button>
<Button
title={'跳转到热门页面'}
onPress={()=>{
console.log(this.props)
this.props.navigation.navigate('Hot',{
title:'热门'
})
}}
></Button>
<Text style={styles.text}>HomePage</Text>
留意右上角,原生按钮已经支持返回。但我想自定义一个返回按钮的话可以直接用goBack方法:
<Button
title={'返回'}
onPress={()=>{
this.props.navigation.goBack()
}}
></Button>
goBack:function goBack(key):我们可以借助goBack返回到上⼀页或者路由栈的指定⻚面。
key标识你要返回到⻚面的⻚面标识符,如:id-1517035332238-4,不是routeName。可以通过指定⻚面的navigation.state.key来获取⻚页⾯面的标识 key必传,不传默认返回上一页
现在我想定义一个参数给下一个也页面,可以这么做:
<Button
title={'跳转到热门页面'}
onPress={()=>{
console.log(this.props)
this.props.navigation.navigate('Hot',{
title:'热门'
})
}}
></Button>
在被跳转的页面可以通过getParam方法获取:
<Text style={styles.text}>{this.props.navigation.getParam('title')}</Text>
留意到以下模拟器中,
这些都是可以自己定义的。
回到AppNavigator,我可以给首页加个标题:
Home:{
screen:HomePage,
// 导航器配置:
navigationOptions:{
// 让导航栏为空
// header:null
// 设置导航栏标题
headerTitle:'首页'
}
},
你还可以自定义navigationOptions:
export default class MyPage extends Component{
static navigationOptions={
title:'我的'
}
render(){
return (
<View style={styles.container}>
<Text style={styles.text}>{this.props.navigation.getParam('title')}</Text>
</View>
)
}
}
接下来可以组合页面穿参来用:
static navigationOptions=({navigation})=>{
return {
headerTitle:navigation.getParam('title')
}
}
!image-20190914172536556
有getParam就有setParams。
export default class MyPage extends Component{
static navigationOptions=({navigation})=>{
return {
headerTitle:navigation.getParam('title')
}
}
render(){
return (
<View style={styles.container}>
<Text style={styles.text}>{this.props.navigation.getParam('title')}</Text>
<Button
title={'更新页面参数'}
onPress={()=>{
this.props.navigation.setParams({
title:'aaa'
})
}}
></Button>
</View>
)
}
}
一点击,你的title就全变成了aaa。
注意:navigation.setParams改变的是当前页⾯的Params,如果要改变其他⻚面的Params可以通过 NavigationActions.setParams完成。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有