【REACT NATIVE 系列教程之五】NAVIGATOR(页面导航)的基本使用与传参

前端之家收集整理的这篇文章主要介绍了【REACT NATIVE 系列教程之五】NAVIGATOR(页面导航)的基本使用与传参前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本站文章均为李华明Himi原创,转载务必在明显处注明:
转载自【黑米GameDev街区】 原文链接:http://www.himigame.com/react-native/2248.html


今天介绍一种应用开发中常用的负责页面切换及导航功能的组件:Navigator

一:Navigator

对于页面导航其实主要功能就是:每个页面都知道本身应该切换到哪个页面,并且切到的页面会记录从哪里来,如果要返回的话,知道返回到哪个页面。这一切都不需要再用逻辑管理!而且每个页面之间也可以进行参数传递,很方便的组件。废话不多说:先上示例代码

首先我们导入Navigator 组件:

  1. importReact,{
  2. ...
  3. Navigator,...
  4. }from'react-native';

使用方式:

  1. render(){
  2. return(
  3. <Navigator
  4. initialRoute={{name:'FirstPage',component:FirstPage}}
  5. configureScene={(route)=>{
  6. returnNavigator.SceneConfigs.HorizontalSwipeJump;
  7. }}
  8. renderScene={(route,navigator)=>{
  9. letComponent=route.component;
  10. return<Component{...route.params}navigator={navigator}/>
  11. }}
  12. />
  13. )}


上面的代码,初始化了导航组件,且设置默认显示页面为 FirstPage,这里需要注意两点:

1. 我们主要关注Navigator 里 initialRoute 中的 两个参数:

name: 组件名

component: 组件Class名

Himi导入FirstPage时如下:

  1. importFirstPagefrom'./FirstPage'

所以 name 和 component 都填写的为FirstPage

2.<Component {…route.params} navigator={navigator} />

上面这一行是将navigator作为props进行传递

3. Navigator.SceneConfigs.HorizontalSwipeJump

上面一行是设置页面切换之间的动画效果,更多效果查看源文件

node_modules/react-native/Libraries/CustomComponents/Navigator/NavigatorSceneConfigs.js

下面我们看下FirstPage.js:

  1. importReact,{
  2. AppRegistry,Component,View,Text,StyleSheet,TouchableHighlight,}from'react-native';
  3. importSecondPagefrom'./SecondPage';
  4. classFirstPageextendsReact.Component{
  5. constructor(props){
  6. super(props);
  7. this.state={};
  8. }
  9. nextEvent(){
  10. const{navigator}=this.props;
  11. if(navigator){
  12. navigator.push({
  13. name:'SecondPage',component:SecondPage,params:{
  14. titleText:''
  15. }
  16. })
  17. }
  18. }
  19. render(){
  20. return(
  21. <Viewstyle={styles.himiViewStyle}>
  22. <Textstyle={styles.himiTextStyle}>HimiReactNative教程</Text>
  23. <Viewstyle={styles.himiViewStyle}>
  24. <TouchableHighlight
  25. underlayColor='#4169e1'
  26. onPress={this.nextEvent.bind(this)}
  27. >
  28. <Textstyle={styles.text}>点击我进入[SecondPage]个页面</Text>
  29. </TouchableHighlight>
  30. </View>
  31. </View>
  32. )}
  33. };
  34. varstyles=StyleSheet.create({
  35. text:{
  36. color:'#f00',fontSize:20,},himiViewStyle:{
  37. flex:1,flexDirection:'column',justifyContent:'center',alignItems:'center',backgroundColor:'#F5FCFF',himiTextStyle:{
  38. color:'#f00',fontSize:30,marginTop:70,});
  39. module.exports=FirstPage;

这里我们主要看 nextEvent 函数内容

const { navigator } = this.props; 这一句是以props传递过来的navigator进行接收。

得到Navigator组件,可以利用其 push 与pop两个函数进行切换下一页面与回到上个页面操作。

下面代码段演示了如何切换到下一个页面

  1. if(navigator){//判断是否正常接收到传来的导航组件
  2. navigator.push({//利用此函数进行切换到指定页面
  3. name:'SecondPage',//目标组件名
  4. component:SecondPage,//目标组件Class名
  5. params:{
  6. titleText:''
  7. }
  8. })
  9. }


还需要强调的是params,此参数是页面切换时传入下个页面的参数书写形式。

获取时,直接 this.props.titleText 即可得到,简单、方便。

下面代码段演示如何返回上个页面

  1. const{navigator}=this.props;
  2. if(navigator){
  3. navigator.pop();
  4. }

返回上一页面就十分简单了,不多赘述了。

Himi这里为了演示效果,所以下面把ThreePage.js也给出源码:(最后附上动态效果图)

  1. importReact,Alert,}from'react-native';
  2. exportdefaultclassthreePageextendsReact.Component{
  3. constructor(props){
  4. super(props);
  5. this.state={};
  6. }
  7. backEvent(){
  8. const{navigator}=this.props;
  9. if(navigator){
  10. navigator.pop();
  11. }
  12. }
  13.  
  14. render(){
  15. return(
  16. <Viewstyle={styles.himiViewStyle}>
  17. <Textstyle={styles.himiTextStyle}>ThreePage</Text>
  18.  
  19. <Viewstyle={styles.himiViewStyle}>
  20. <TouchableHighlight
  21. underlayColor='#4169e1'
  22. onPress={this.backEvent.bind(this)}
  23. >
  24. <Textstyle={styles.text}>返回[SecondPage]页面</Text>
  25. </TouchableHighlight>
  26. </View>
  27. </View>
  28. )}
  29. };
  30. varstyles=StyleSheet.create({
  31. text:{
  32. color:'#f00',});


运行效果图:(点击查看动态效果

user23

从上图效果可以看出,切换之间的页面可以直接通过手势滑动进行切换~:)

猜你在找的React相关文章