RN路由stackNavigator以及TabNavigator的相互嵌套

簡單的小案例。在首頁上有選項卡和頁面跳轉的功能。從首頁跳到第二個頁面,第二個頁面可以跳轉到第三四個頁面

參考網站 https://reactnavigation.org/docs/navigators/stack

1.index.android.js
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    View,
    Text,
    Button,
} from 'react-native';
import Tab from './tab';
import Stack from './src/Stack/StackNavigator';
import TabNavigator from  './src/Stack/TabNavigator';
export default class Login extends Component {
  render() {
    return (   
        <TabNavigator/>
    );
  }
}
AppRegistry.registerComponent('Login', () => Login);

2.TabNavigator.js

 import React, { Component } from 'react';
import {
   AppRegistry,
   StyleSheet,
   View,
   Text,
   Button,
   Image
} from 'react-native';
import {TabNavigator,StackNavigator } from 'react-navigation';
import Two from './Two';
class MyHomeScreen extends Component {
   static navigationOptions = {
       tabBarLabel: '首頁',
       tabBarIcon: ({tintColor})=>(
           <Image
               source={require('../logo.png')}
               style={[style.icon, {tintColor: tintColor}]}
           />
       ),
       header:null
   };
   render() {
       return (
           <View>
               <Text>第一個頁面</Text>
               <Button
                   onPress={()=>this.props.navigation.navigate('Two')}
                   title="去第二個頁面"
               />
           </View>

       );
   };
}
class MyNotificationsScreen extends Component{
   static  navigationOptions={
       tabBarLabel:'通知頁',
       tabBarIcon:({tintColor})=>(
           <Image
               source={require('../logo.png')}
               style={[styles.icon,{tintColor:tintColor}]}
           />
       ),
       header:null
   };
   render(){
       return(
           <Text>通知頁</Text>
       );
   }
}
const styles=StyleSheet.create({
   icon:{
       width:26,
       height:26,
   }
});

const MyApp=TabNavigator({
   Home:{screen:MyHomeScreen},
   Notifications:{screen:MyNotificationsScreen},
},{
   tabBarOptions:{
       activeTintColor:'#e91e63',
   },
   header:'null'
});
const SimpleApp=StackNavigator({
   Home:{screen:MyApp},
   Two:{screen:Two}
});
module.exports=SimpleApp;
  1. Two.js
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    View,
    Text,
    Button,
} from 'react-native';
import {StackNavigator} from 'react-navigation';
import Home from './TabNavigator';
class Two extends Component {
    static navigationOptions={
        header:null
    };
    render() {
        return (
            <View>
                <Text>第二個頁面</Text>
                <Button
                  onPress={()=>this.props.navigation.navigate('Home')}
                  title="返回"
                />
                <Button
                    onPress={()=>this.props.navigation.navigate('Three')}
                    title="去第三個頁面"
                />
                <Button
                  onPress={()=>this.props.navigation.navigate('Four')}
                  title="去第四個頁面"
                />
            </View>
        );
    }
}
class Three extends Component {
    static navigationOptions={
        header:null
        // mode:'card',
    };
    render() {
        return (
            <View style={{flex:1,}}>
                <Button
                  onPress={()=>this.props.navigation.goBack()}
                  title="返回"
                />
                <Text>第三個頁面</Text>
            </View>
        );
    }
}
class Four extends Component {
    static navigationOptions={
        header:null
        // mode:'card',
    };
    render() {
        return (
            <View style={{flex:1,}}>
                <Button
                    onPress={()=>this.props.navigation.goBack()}
                    title="返回"
                />
                <Text>第四個頁面</Text>
            </View>
        );
    }
}
const MyApp=StackNavigator({
   Two:{screen:Two},
    Three:{screen:Three},
    Four:{screen:Four}
});
module.exports=MyApp;

以上代碼就是一個簡單的小demo。因為剛學習,可能有很多地方寫的不是很規范。希望各位可以提出,一起學習

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評論 25 708
  • 美麗定格 注入骨髓里
    多彩北舞多姿時代閱讀 139評論 0 0
  • 踏過一級又一級的青石板臺階、撫過一個又一個欄桿上的漢白玉浮雕、看過一幅又一幅讓人頓悟的偈語……小兒只管一路跑跳,歡...
    小鹿says閱讀 220評論 0 2
  • 大年初一三件事情,上午去拜年。 下午去玩。 晚上包餃子。 這對于我來說也是一個考驗。客觀分析娘的狀況,哥哥的張狂,...
    lygly9閱讀 286評論 0 0