(??)012_ReactNative: Navigators

(问渠那得清如许,为有源头活水来。 双手奉上RN官网)

Navigators: 多界面之间切换

  • 关于Scenes场景概念(可以简单理解为一屏的展现)

①创建一个名为"MyScene.js"的新文件,并写入以下内容

import React, { Component, PropTypes } from 'react';
import { View, Text, TouchableHighlight } from 'react-native';

//export defaul 声明使得该组件可以被其他组件导入使用
export default class MyScene extends Component {
  static propTypes = {
    title: PropTypes.string.isRequired,
    onForward: PropTypes.func.isRequired,
    onBack: PropTypes.func.isRequired,
  }
  render() {
    return (
      <View>
        <Text>Current Scene: { this.props.title }</Text>
        <TouchableHighlight onPress={this.props.onForward}>
          <Text>Tap me to load the next scene</Text>
        </TouchableHighlight>
        <TouchableHighlight onPress={this.props.onBack}>
          <Text>Tap me to go back</Text>
        </TouchableHighlight>
      </View>
    )
  }
}

②可以在index中引入

import React, { Component } from 'react';
import { AppRegistry, Navigator, Text, View } from 'react-native';

import MyScene from './MyScene';

class SimpleNavigationApp extends Component {
  render() {
    return (
      <Navigator
        initialRoute={{ title: 'My Initial Scene', index: 0 }}
        renderScene={(route, navigator) =>
          <MyScene
            title={route.title}

            // Function to call when a new scene should be displayed           
            onForward={ () => {    
              const nextIndex = route.index + 1;
              navigator.push({
                title: 'Scene ' + nextIndex,
                index: nextIndex,
              });
            }}

            // Function to call to go back to the previous scene
            onBack={() => {
              if (route.index > 0) {
                navigator.pop();
              }
            }}
          />
        }
      />
    )
  }
}

AppRegistry.registerComponent('ReactNativeLearn', () => SimpleNavigationApp);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • (问渠那得清如许,为有源头活水来。 双手奉上RN官网) Integration With Existing App...
    莫_名阅读 2,635评论 0 0
  • (问渠那得清如许,为有源头活水来。 双手奉上RN官网) 在index.ios.js中写入 你需要理解一些基础的Re...
    莫_名阅读 2,405评论 0 0
  • 基本概念(问渠那得清如许,为有源头活水来。 双手奉上RN官网) react|—react.js(web端js框架,...
    莫_名阅读 2,823评论 0 0
  • (问渠那得清如许,为有源头活水来。 双手奉上RN官网) Images 图片 静态图片: 可以使用如 的方式获取加载...
    莫_名阅读 1,657评论 0 0
  • (问渠那得清如许,为有源头活水来。 双手奉上RN官网) 固定尺寸: Height and Width 用于确定组件...
    莫_名阅读 2,338评论 0 0

友情链接更多精彩内容