本站文章均为李华明Himi原创,转载务必在明显处注明:
转载自【黑米GameDev街区】 原文链接:http://www.himigame.com/react-native/2248.html
今天介绍一种应用开发中常用的负责页面切换及导航功能的组件:Navigator
一:Navigator
对于页面导航其实主要功能就是:每个页面都知道本身应该切换到哪个页面,并且切到的页面会记录从哪里来,如果要返回的话,知道返回到哪个页面。这一切都不需要再用逻辑管理!而且每个页面之间也可以进行参数传递,很方便的组件。废话不多说:先上示例代码:
首先我们导入Navigator 组件:
- importReact,{
- ...
- Navigator,...
- }from'react-native';
使用方式:
- render(){
- return(
- <Navigator
- initialRoute={{name:'FirstPage',component:FirstPage}}
- configureScene={(route)=>{
- returnNavigator.SceneConfigs.HorizontalSwipeJump;
- }}
- renderScene={(route,navigator)=>{
- letComponent=route.component;
- return<Component{...route.params}navigator={navigator}/>
- }}
- />
- )}
上面的代码,初始化了导航组件,且设置默认显示页面为 FirstPage,这里需要注意两点:
1. 我们主要关注Navigator 里 initialRoute 中的 两个参数:
name: 组件名
component: 组件Class名
Himi导入FirstPage时如下:
- 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:
- importReact,{
- AppRegistry,Component,View,Text,StyleSheet,TouchableHighlight,}from'react-native';
- importSecondPagefrom'./SecondPage';
- classFirstPageextendsReact.Component{
- constructor(props){
- super(props);
- this.state={};
- }
- nextEvent(){
- const{navigator}=this.props;
- if(navigator){
- navigator.push({
- name:'SecondPage',component:SecondPage,params:{
- titleText:''
- }
- })
- }
- }
- render(){
- return(
- <Viewstyle={styles.himiViewStyle}>
- <Textstyle={styles.himiTextStyle}>HimiReactNative教程</Text>
- <Viewstyle={styles.himiViewStyle}>
- <TouchableHighlight
- underlayColor='#4169e1'
- onPress={this.nextEvent.bind(this)}
- >
- <Textstyle={styles.text}>点击我进入[SecondPage]个页面</Text>
- </TouchableHighlight>
- </View>
- </View>
- )}
- };
- varstyles=StyleSheet.create({
- text:{
- color:'#f00',fontSize:20,},himiViewStyle:{
- flex:1,flexDirection:'column',justifyContent:'center',alignItems:'center',backgroundColor:'#F5FCFF',himiTextStyle:{
- color:'#f00',fontSize:30,marginTop:70,});
- module.exports=FirstPage;
const { navigator } = this.props; 这一句是以props传递过来的navigator进行接收。
得到Navigator组件,可以利用其 push 与pop两个函数进行切换下一页面与回到上个页面操作。
还需要强调的是params,此参数是页面切换时传入下个页面的参数书写形式。
获取时,直接 this.props.titleText 即可得到,简单、方便。
- const{navigator}=this.props;
- if(navigator){
- navigator.pop();
- }
返回上一页面就十分简单了,不多赘述了。
Himi这里为了演示效果,所以下面把ThreePage.js也给出源码:(最后附上动态效果图)
- importReact,Alert,}from'react-native';
- exportdefaultclassthreePageextendsReact.Component{
- constructor(props){
- super(props);
- this.state={};
- }
- backEvent(){
- const{navigator}=this.props;
- if(navigator){
- navigator.pop();
- }
- }
- render(){
- return(
- <Viewstyle={styles.himiViewStyle}>
- <Textstyle={styles.himiTextStyle}>ThreePage</Text>
- <Viewstyle={styles.himiViewStyle}>
- <TouchableHighlight
- underlayColor='#4169e1'
- onPress={this.backEvent.bind(this)}
- >
- <Textstyle={styles.text}>返回[SecondPage]页面</Text>
- </TouchableHighlight>
- </View>
- </View>
- )}
- };
- varstyles=StyleSheet.create({
- text:{
- color:'#f00',});