react native 页面跳转
React Native目前有几个内置的导航器组件,一般来说我们首推Navigator。它使用纯JavaScript实现了一个导航栈,因此可以跨平台工作
场景简单来说其实就是一个全屏的React组件。与之相对的是单个的Text、Image又或者是你自定义的什么组件,仅仅占据页面中的一部分。
下面我们来定义一个仅显示一些文本的简单场景。创建一个名为“MyScene.js”的文件,然后粘贴如下代码:
import React, { Component } from 'react';注意组件声明前面的
import { View, Text } from 'react-native'; export default class MyScene extends Component {
static defaultProps = {
title: 'MyScene'
}; render() {
return (
<View>
<Text>Hi! My name is {this.props.title}.</Text>
</View>
)
}
}export default关键字。它的意思是导出(export)当前组件,以允许其他组件引入(import)和使用当前组件
// ./MyScene表示的是当前目录下的MyScene.js文件,也就是我们刚刚创建的文件
// 注意即便当前文件和MyScene.js在同一个目录中,"./"两个符号也是不能省略的!
// 但是.js后缀是可以省略的
import MyScene from './MyScene';
class YoDawgApp extends Component {
render() {
return (
<MyScene />
)
}
}
AppRegistry.registerComponent('YoDawgApp', () => YoDawgApp);
场景已经说的够多了,下面我们开始尝试导航跳转。首先要做的是渲染一个Navigator组件,然后通过此组件的renderScene属性方法来渲染其他场景。
render() {
return (
<Navigator
initialRoute={{ title: 'My Initial Scene', index: 0 }}
renderScene={(route, navigator) => {
return <MyScene title={route.title} />
}}
/>
);
}
使用导航器经常会碰到“路由(route)”的概念。“路由”抽象自现实生活中的路牌,在RN中专指包含了场景信息的对象。renderScene方法是完全根据路由提供的信息来渲染场景的。你可以在路由中任意自定义参数以区分标记不同的场景,我们在这里仅仅使用title作为演示。
要过渡到新的场景,你需要了解push和pop方法。这两个方法由navigator对象提供,而这个对象就是上面的renderScene方法中传递的第二个参数。 我们使用这两个方法来把路由对象推入或弹出导航栈
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('SimpleNavigationApp', () => SimpleNavigationApp);
对应的MyScene.js代码如下:
import React, { Component, PropTypes } from 'react';
import { View, Text, TouchableHighlight } from 'react-native';
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>点我进入下一场景</Text>
</TouchableHighlight>
<TouchableHighlight onPress={this.props.onBack}>
<Text>点我返回上一场景</Text>
</TouchableHighlight>
</View>
)
}
}
react native 页面跳转的更多相关文章
- [RN] React Native 滚动跳转到指定位置
React Native 滚动跳转到指定位置 一.结构 <ScrollView horizontal={true} ref={(view) => { this.myScrollView = ...
- [RN] React Native FlatList跳转到顶部/底部
React Native FlatList跳转到顶部/底部 核心代码如下: <ScrollView showsVerticalScrollIndicator={false} contentCon ...
- react native jpush跳转页面不成功解决方法
在点击事件时加入如下红色代码即可 import JPushModule from 'jpush-react-native'; ... componentDidMount() { // 新版本必需写回调 ...
- React native路由跳转navigate、push、replace的区别
由于没有系统的去学习RN,对路由跳转了解不多,只是跟着项目在做,抽点时间简单学习一下RN路由跳转方法区别,总结如下: 如上图,外部是一个栈容器,此时A页面在最底部,navigate到B页面,为什么此时 ...
- react native 1跳2 2跳3 3跳4 4pop回2
网上有介绍导航的很多了 就不一一说了 直接说一个小功能 popToRoute pop回指定页面 第一次写 组织能力不是特别好 直接贴代码 例如 我们有四个页面 从第四个pop到第二个页面 先 ...
- React Native导航器之react-navigation使用
在上一节Navigation组件,我们使用系统提供的导航组件做了一个跳转的例子,不过其实战能力不强,这里推荐一个超牛逼的第三方库:react-navigation.在讲react-navigation ...
- [书籍精读]《React Native精解与实战》精读笔记分享
写在前面 书籍介绍:本书由架构师撰写,包含ReactNative框架底层原理,以及与iOS.Android混合开发案例,精选了大量实例代码,方便读者快速学习.主要内容分为两大部分,第1部分" ...
- 【React Native】在原生和React Native间通信(RN调用原生)
一.从React Native中调用原生方法(原生模块) 原生模块是JS中也可以使用的Objective-C类.一般来说这样的每一个模块的实例都是在每一次通过JS bridge通信时创建的.他们可以导 ...
- React Native学习(三)—— 使用导航器Navigation跳转页面
本文基于React Native 0.52 参考文档https://reactnavigation.org/docs/navigators/navigation-prop 一.基础 1.三种类型 Ta ...
随机推荐
- Android的学习第六章(布局一LinearLayout)
今天我们来说一下Android五大布局-LinearLayout布局(线性布局) 含义:线性布局,顾名思义,指的是整个Android布局中的控件摆放方式是以线性的方式摆放的, 主要作用:主要对整个界面 ...
- pysvn安装及常用方法
centos 6.5,svn 1.6.11,pysvn 1.7.6,文章内容来自官网文档:http://pysvn.tigris.org/docs/pysvn_prog_guide.html 直接用y ...
- 【leetcode❤python】 165. Compare Version Numbers
#-*- coding: UTF-8 -*-class Solution(object): def compareVersion(self, version1, version2): ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- 聊聊 virtualenv 和 virtualenvwrapper 实践
各位 Python 的小伙伴肯定多多少少接触过 virtualenv.本文将介绍 virtualenv 以及如何更科学更优雅地使用 virtualenv. virtualenv 首先来聊一下 virt ...
- shmop ftok
http://blog.csdn.net/heiworld/article/details/25426723 对于ftok的理解 http://www.jb51.net/article/510 ...
- SQL出错
2016-12-26 15:43:02,870 DEBUG [org.springframework.test.context.support.DirtiesContextTestExecutionL ...
- springboot中swaggerUI的使用
demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...
- 修复docker pull image failed
修复docker pull image failed docker pull报错 message":"Get https://n6-026-137.byted.org/v1/_pi ...
- 1005. Spell It Right (20)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...