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

本文主要是介绍react-native使用react-navigation进行页面跳转导航,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先要确认已经配置好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><ButtononPress={() => 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><ButtononPress={() => 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 (<Buttontitle="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><ButtononPress={() => navigate('Three')}title="to to ThreeScreen"/></View>);}}

 

最后的结果如下:

 

 

 

 

 

 

 

最后给出完整代码

 

文件 index.android.js

i

mport 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><ButtononPress={() => 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><ButtononPress={() => 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 (<Buttontitle="Go back"onPress={() => goBack()}/>);}}export default Three;

 

 

这篇关于react-native使用react-navigation进行页面跳转导航的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1044167

相关文章

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

Go语言中Recover机制的使用

《Go语言中Recover机制的使用》Go语言的recover机制通过defer函数捕获panic,实现异常恢复与程序稳定性,具有一定的参考价值,感兴趣的可以了解一下... 目录引言Recover 的基本概念基本代码示例简单的 Recover 示例嵌套函数中的 Recover项目场景中的应用Web 服务器中

html 滚动条滚动过快会留下边框线的解决方案

《html滚动条滚动过快会留下边框线的解决方案》:本文主要介绍了html滚动条滚动过快会留下边框线的解决方案,解决方法很简单,详细内容请阅读本文,希望能对你有所帮助... 滚动条滚动过快时,会留下边框线但其实大部分时候是这样的,没有多出边框线的滚动条滚动过快时留下边框线的问题通常与滚动条样式和滚动行

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

SpringBoot3中使用虚拟线程的完整步骤

《SpringBoot3中使用虚拟线程的完整步骤》在SpringBoot3中使用Java21+的虚拟线程(VirtualThreads)可以显著提升I/O密集型应用的并发能力,这篇文章为大家介绍了详细... 目录1. 环境准备2. 配置虚拟线程方式一:全局启用虚拟线程(Tomcat/Jetty)方式二:异步

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意

使用Python实现获取屏幕像素颜色值

《使用Python实现获取屏幕像素颜色值》这篇文章主要为大家详细介绍了如何使用Python实现获取屏幕像素颜色值,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、一个小工具,按住F10键,颜色值会跟着显示。完整代码import tkinter as tkimport pyau

Linux使用scp进行远程目录文件复制的详细步骤和示例

《Linux使用scp进行远程目录文件复制的详细步骤和示例》在Linux系统中,scp(安全复制协议)是一个使用SSH(安全外壳协议)进行文件和目录安全传输的命令,它允许在远程主机之间复制文件和目录,... 目录1. 什么是scp?2. 语法3. 示例示例 1: 复制本地目录到远程主机示例 2: 复制远程主

Java Lambda表达式的使用详解

《JavaLambda表达式的使用详解》:本文主要介绍JavaLambda表达式的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、前言二、Lambda表达式概述1. 什么是Lambda表达式?三、Lambda表达式的语法规则1. 无参数的Lambda表