Có rất nhiều thư viện khác nhau để làm navigation với React native, mình khuyến khích các bạn dùng react-navigation của Facebook phát triển cho React Native, nói chung cái gì đã có bản official của chính người sáng lập thì mình cứ đè nó ra xài
Stack
Khi mở một màn hình mới, nó sẽ sếp chồng lên trên màn hình trước đó. Kiếu sếp bánh tráng.
Theo mặc định, màn hình mới chạy từ cạnh phải -> qua trái màn hình trong iOS, mờ -> đến rõ dần từ dưới lên trong android
TouchableMenuIcon là component luôn hiển thị ở gốc trái của ứng dụng
Chạm vào icon, SideMenu component được hiển thị, style tùy thích
RootStack là một Stack Navigator chứa các màn hình chính của ứng dụng
MyDrawerNavigator tạo ra từ createDrawerNavigator(), sẽ đảm nhiệm việc show side menu, để ý cái propscontentComponent
importReact,{Component}from'react';import{Button,View,Text,Image,TouchableOpacity}from'react-native';import{ createStackNavigator, createDrawerNavigator }from'react-navigation';import{RootStackScreen1,RootStackScreen2,RootStackScreen3}from'./screens';// 1) `TouchableMenuIcon` là component luôn hiển thị ở gốc trái của ứng dụngclassTouchableMenuIconextendsComponent{
toggleDrawer=()=>{this.props.navigationProps.toggleDrawer();}
render(){return(<View style={{flexDirection:'row'}}><TouchableOpacity onPress={this.toggleDrawer.bind(this)}><Image
source={{uri :'https://reactnativecode.com/wp-content/uploads/2018/04/hamburger_icon.png'}}
style={{ width:25, height:25, marginLeft:5}}/></TouchableOpacity></View>);}}// 2) Chạm vào icon, `SideMenu` component được hiển thị, style tùy thíchclassSideMenuextendsComponent{
render(){return(<View style={{flex:1, backgroundColor:'white'}}><View style={{flexDirection:'column', marginTop:30, justifyContent:'space-around'}}><Button
title="Screen 1"
onPress={()=>{this.props.navigation.navigate('RootStackScreen1');this.props.navigation.closeDrawer();}}/><Button
title="Screen 2"
onPress={()=>{this.props.navigation.navigate('RootStackScreen2');this.props.navigation.closeDrawer();}}/><Button
title="Screen 3"
onPress={()=>{this.props.navigation.navigate('RootStackScreen3');this.props.navigation.closeDrawer();}}/></View></View>);}}// 3) `RootStack` là một Stack Navigator chứa các màn hình chính của ứng dụngconstRootStack= createStackNavigator({RootStackScreen1:RootStackScreen1,RootStackScreen2:RootStackScreen2,RootStackScreen3:RootStackScreen3},{
initialRouteName:'RootStackScreen1',
navigationOptions:({ navigation })=>({
title:"Root Stack",
headerLeft:<TouchableMenuIcon navigationProps={ navigation }/>})});// 4) `MyDrawerNavigator` tạo ra từ `createDrawerNavigator()`, sẽ đảm nhiệm việc show side menu, để ý cái `props` **contentComponent**exportdefaultMyDrawerNavigator= createDrawerNavigator({RootStack:RootStack,},{
contentComponent:SideMenu});