[React Native] React Navigation使用

1. Hello Mobile Navigation

  • Stack Navigator简介
    基本导航栏样式如下;
1.png

代码如下所示:
![Uploading Simulator Screen Shot 2017年7月14日 上午11.26.28_508132.png . . .]

import React , {Component} from 'react';
import {
  AppRegistry,  
  View,
  StyleSheet,
  Text
} from 'react-native';
import {StackNavigator} from 'react-navigation' 

export default class HomeScreen extends Component{
  static navigationOptions = {
    title: 'Welcome',
  };
  render(){
    return( 
      <View style={styles.container}> 
        <Text>Hello, world!</Text>
      </View>
    )
  }
}
//初始化导航栏
const SimpleApp = StackNavigator({
  Home: {screen: HomeScreen}
});

const styles = StyleSheet.create({
  container:{ 
    flex: 1, 
    paddingLeft: 10,
    paddingRight: 10,
  }  
})

AppRegistry.registerComponent('thirtyDayOfReactNative', () => SimpleApp);
  • 界面跳转
2.png

3.png
import React , {Component} from 'react';
import {
  AppRegistry,  
  View,
  StyleSheet,
  Text,
  Button
} from 'react-native';
import {StackNavigator} from 'react-navigation' 

export class ChatScreen extends Component {
   static navigationOptions = {
    title: 'chat with lucy',
  };
  render() {
    return (
      <View>
        <Text>Chat with Lucy</Text>
      </View>
    );
  }
}

export default class HomeScreen extends Component{
  static navigationOptions = {
    title: 'Welcome',
  };
  render(){
    const {navigate} = this.props.navigation;
    return( 
      <View> 
        <Text>Hello, Chat App!</Text>
        <Button 
          onPress={() => navigate('Chat')}
          title='Chat with Lucy'
        />
      </View> 
    )
  }
}
const SimpleApp = StackNavigator({
  Home: {screen: HomeScreen},
  Chat: {screen: ChatScreen},
}); 

AppRegistry.registerComponent('thirtyDayOfReactNative', () => SimpleApp);
  • 页面直接传递参数
export class ChatScreen extends Component { 
  static navigationOptions = ({ navigation }) => ({  
    title: 'Chat with ' + navigation.state.params.user,
  });
  render() {
    const {params} = this.props.navigation.state;
    return (
      <View>
        <Text>Chat with {params.user}</Text>
      </View>
    );
  }
}

export default class HomeScreen extends Component{
  static navigationOptions = {
    title: 'Welcome',
  };
  render(){
    const {navigate} = this.props.navigation;
    return( 
      <View> 
        <Text>Hello, Chat App!</Text>
        <Button 
          onPress={() => navigate('Chat', {user: 'huage'})}
          title='Chat with huage'
        />
      </View> 
    )
  }
}
const SimpleApp = StackNavigator({
  Home: {screen: HomeScreen},
  Chat: {screen: ChatScreen},
}); 

2. Tab Navigators

export class ChatScreen extends Component {  
  render() { 
    return (
      <View>
        <Text style={{marginTop:20}}>Chat with huage</Text>
      </View>
    );
  }
}

export default class HomeScreen extends Component{ 
  render(){ 
    return( 
      <View> 
        <Text style={{marginTop:20}}>Hello, Chat App!</Text> 
      </View> 
    )
  }
}
const SimpleApp = TabNavigator({
  Home: {screen: HomeScreen},
  Chat: {screen: ChatScreen},
}); 

AppRegistry.registerComponent('thirtyDayOfReactNative', () => SimpleApp);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、开源库介绍 今年1月份,新开源的React-natvigation库备受瞩目。在短短不到3个月的时间,gith...
    德山_阅读 2,246评论 0 19
  • title: 翻译|React-navigation导航系统(2)date: 2017-03-28 07:48:3...
    smartphp阅读 7,976评论 2 23
  • 这篇文章来自npm react-native-navigation 中的说明。 git上的文档和源码不匹配,所以贴...
    taiji1985阅读 6,568评论 1 6
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,980评论 25 709
  • 执笔描红,偷画琴筝,翠园绿竹,花田半亩。留待对月良人寄相思。当日丝弦随云去,念此时情苦。夜来风雨,倾城独舞。 月落...
    慕水阅读 342评论 2 10