react-native使用react-navigation进行页面跳转导航的示例


Posted in Javascript onSeptember 07, 2017

首先要确认已经配置好react-native的环境。

# 创建一个native应用,SimpleApp,然后进入项目目录
react-native init SimpleApp
cd SimpleApp


# 通过npm安装最新版本的react-navigation
npm install --save react-navigation


# 运行程序
react-native run-android

引入Stack Navigator

对于我们的应用程序,我们想要使用堆栈式导航器,因为我们想要一个概念的“堆栈”导航,其中每个新屏幕都放在堆栈顶部,然后从堆栈顶部移除一个屏幕。

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

class HomeScreen extends React.Component {
 static navigationOptions = {
  title: 'Welcome world',
 };
 render() {
  return <Text>Hello, Navigation!</Text>;
 }
}
const SimpleApp = StackNavigator({
 Home: { screen: HomeScreen },
});

AppRegistry.registerComponent('SimpleApp', () => SimpleApp);

屏幕的title在静态导航选项中是可配置的,在这里可以设置许多选项来配置导航器中的屏幕显示。

添加一个新的屏幕

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

然后在HomeScreen添加一个按钮,链接到ChatScreen

class HomeScreen extends React.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>
  );
 }

最后将添加的两个页面添加到StackNavigator中

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

在这里,可以传递参数,从HomeScreen传递

class HomeScreen extends React.Component {
 static navigationOptions = {
  title: 'Welcome',
 };
 render() {
  const { navigate } = this.props.navigation;
  return (
   <View>
    <Text>Hello, Chat App!</Text>
    <Button
     onPress={() => navigate('Chat', { user: 'Lucy' })}
     title="Chat with Lucy"
    />
   </View>
  );
 }
}

ChatScreen接收参数

class ChatScreen extends React.Component {
 // Nav options can be defined as a function of the screen's props:
 static navigationOptions = ({ navigation }) => ({
  title: `Chat with ${navigation.state.params.user}`,
 });
 render() {
  // The screen's current route is passed in to `props.navigation.state`:
  const { params } = this.props.navigation.state;
  return (
   <View>
    <Text>Chat with {params.user}</Text>
   </View>
  );
 }
}

添加第三个页面,Three.js, ChatScreen跳转到Three

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

class Three extends React.Component {
 static navigationOptions = {
  title: 'Three Sceen',
 };
 render() {
  const { goBack } = this.props.navigation;
  return (
   <Button
    title="Go back"
    onPress={() => goBack()}
   />
  );
 }
}
export default Three;

修改ChatScreen的配置

class ChatScreen
extends React.Component {

static navigationOptions = {

title: 'Chat with Lucy',

};

render() {

const { navigate } =
this.props.navigation;

return (

<View>

<Text>Chat with Lucy</Text>

<Button

onPress={() =>
navigate('Three')}

title="to to ThreeScreen"

/>

</View>

);

}

}

最后的结果如下:

react-native使用react-navigation进行页面跳转导航的示例 

react-native使用react-navigation进行页面跳转导航的示例 

react-native使用react-navigation进行页面跳转导航的示例 

最后给出完整代码

文件 index.android.js

import SimpleApp
from './App';

文件App.js

import React
from 'react';

import {

AppRegistry,

Text,

View,

Button,

} from 'react-native';

import { StackNavigator }
from 'react-navigation';

import ThreeScreen
from './Three.js';

 

class HomeScreen
extends React.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>

);

}

}

 

class ChatScreen
extends React.Component {

static navigationOptions = {

title: 'Chat with Lucy',

};

render() {

const { navigate } =
this.props.navigation;

return (

<View>

<Text>Chat with Lucy</Text>

<Button

onPress={() =>
navigate('Three')}

title="to to ThreeScreen"

/>

</View>

);

}

}

 

const SimpleApp =
StackNavigator({

Home: { screen:
HomeScreen },

Chat: { screen:
ChatScreen },

Three: { screen:
ThreeScreen},

});

 

AppRegistry.registerComponent('SimpleApp', ()
=> SimpleApp);

文件Three.js

import React,{Component}
from 'react';

import {

AppRegistry,

Text,

View,

Button,

} from 'react-native';

 

class Three
extends React.Component {

static navigationOptions = {

title: 'Three Sceen',

};

render() {

const { goBack } =
this.props.navigation;

return (

<Button

title="Go back"

onPress={() =>
goBack()}

/>

);

}

}

export default
Three;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
基于jquery的一个简单的脚本验证插件
Apr 05 Javascript
用jquery实现的模拟QQ邮箱里的收件人选取及其他效果(一)
Jan 06 Javascript
jquery使用jxl插件导出excel示例
Apr 14 Javascript
JS中常用的输出方式(五种)
Jun 12 Javascript
每日十条JavaScript经验技巧(二)
Jun 23 Javascript
jQuery实现自动调用和触发某个事件的方法
Nov 18 Javascript
jQuery Dialog 打开时自动聚焦的解决方法(两种方法)
Nov 24 Javascript
原生js 封装get ,post, delete 请求的实例
Aug 11 Javascript
vue-cli与webpack处理静态资源的方法及webpack打包的坑
May 15 Javascript
@angular前端项目代码优化之构建Api Tree的方法
Dec 24 Javascript
微信小程序如何实现全局重新加载
Jun 05 Javascript
解决vue-loader加载不上的问题
Oct 21 Javascript
详解vue-cli构建项目反向代理配置
Sep 07 #Javascript
vue数字类型过滤器的示例代码
Sep 07 #Javascript
vue监听scroll的坑的解决方法
Sep 07 #Javascript
react高阶组件经典应用之权限控制详解
Sep 07 #Javascript
React + webpack 环境配置的方法步骤
Sep 07 #Javascript
微信小程序之页面拦截器的示例代码
Sep 07 #Javascript
基于js粘贴事件paste简单解析以及遇到的坑
Sep 07 #Javascript
You might like
php下网站防IP攻击代码,超级实用
2010/10/24 PHP
php cli模式下获取参数的方法
2017/05/05 PHP
php pdo连接数据库操作示例
2019/11/18 PHP
tagName的使用,留一笔
2006/06/26 Javascript
ExtJS 工具栏 分页事件参数
2010/03/05 Javascript
ExtJs使用IFrame的实现代码
2010/03/24 Javascript
js实现同一个页面多个渐变效果的方法
2015/04/10 Javascript
javascript日期操作详解(脚本之家整理)
2015/09/05 Javascript
jQuery实现的指纹扫描效果实例(附演示与demo源码下载)
2016/01/26 Javascript
javaScript基础详解
2017/01/19 Javascript
javascript流程控制语句集合
2017/09/18 Javascript
JS+CSS实现网页加载中的动画效果
2017/10/27 Javascript
windows系统下更新nodejs版本的方案
2017/11/24 NodeJs
微信小程序如何像vue一样在动态绑定类名
2018/04/17 Javascript
js实现列表按字母排序
2020/08/11 Javascript
[37:35]DOTA2上海特级锦标赛A组资格赛#1 Secret VS MVP.Phx第二局
2016/02/25 DOTA
[32:56]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第二场 12.11
2020/12/16 DOTA
python私有属性和方法实例分析
2015/01/15 Python
python中map、any、all函数用法分析
2015/04/21 Python
django通过ajax发起请求返回JSON格式数据的方法
2015/06/04 Python
Python lambda函数基本用法实例分析
2018/03/16 Python
Django开发中的日志输出的方法
2018/07/02 Python
CentOS7下python3.7.0安装教程
2018/07/30 Python
解决python xlrd无法读取excel文件的问题
2018/12/25 Python
Python微医挂号网医生数据抓取
2019/01/24 Python
python各层级目录下import方法代码实例
2020/01/20 Python
学python最电脑配置有要求么
2020/07/05 Python
Python排序函数的使用方法详解
2020/12/11 Python
HTML5 新事件 小结
2009/07/16 HTML / CSS
Notino罗马尼亚网站:购买香水和化妆品
2019/07/20 全球购物
Stührling手表官方网站:男女高品质时尚手表的领先零售商
2021/01/07 全球购物
公司培训欢迎词
2014/01/10 职场文书
大学生的自我鉴定范文
2014/01/21 职场文书
运动会广播稿30字
2014/01/21 职场文书
中学生国旗下讲话稿
2014/04/26 职场文书
芙蓉镇观后感
2015/06/10 职场文书