美文网首页程序员
react native navigation

react native navigation

作者: reedthinking | 来源:发表于2017-06-11 21:42 被阅读0次

使用React Navigation进行跳转,主要有两个页面Home和Chat

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

class HomeScreen extends React.Component {
  static navigationOptions = {
    title: 'Home'
  };
  render() {
    const {navigate} = this.props.navigation;
    return (
      <View style={styles.container}>
        <Text>Hello, this is Home!</Text>
        <Button //用navigate跳转到chat页,传入参数{user:thinkreed}
          onPress= {() => navigate('Chat', {user:'thinkreed'})} title='chat with reed'/>
      </View>
    );
  }
}

class ChatScreen extends React.Component {
  static navigationOptions = ({navigation}) => ({title: `Chat with ${navigation.state.params.user}`});
  render() {
    //传入的{user:'thinkreed'}
    const {params} = this.props.navigation.state;
    return (
      <View style={styles.container}>
        <Text>chat with {params.user}</Text>
      </View>
    );
  }
}

const SimpleApp = StackNavigator({
  //home页
  Home: {
    screen: HomeScreen
  },
  //chat页
  Chat: {
    screen: ChatScreen
  }
});

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  }
});

// 注册应用(registerComponent)后才能正确渲染 注意:只把应用作为一个整体注册一次,而不是每个组件/模块都注册
AppRegistry.registerComponent('DemoProject', () => SimpleApp);

效果如下

Screenshot_20170611-223544.png
Screenshot_20170611-223538.png

相关文章

网友评论

    本文标题:react native navigation

    本文链接:https://www.haomeiwen.com/subject/qtlkqxtx.html