ReactNative——页面跳转
效果图:
进入工作目录,运行
react-native init NavigatorProject
创建项目NavigatorProject
- import React, { Component } from 'react';
- import {
- AppRegistry,
- StyleSheet,
- Text,
- View,
- TouchableHighlight,
- Image,
- Navigator
- } from 'react-native';
- class navigatorProject extends Component{
- render(){
- let defaultName = 'firstPageName';
- let defaultComponent = FirstPageComponent;
- return(
- <Navigator
- initialRoute = {{name: defaultName, component: defaultComponent}} //初始化导航器Navigator,指定默认的页面
- configureScene = {
- (route) => {
- return Navigator.SceneConfigs.FloatFromRight; //配置场景动画,页面之间跳转时候的动画
- }
- }
- renderScene = {
- (route, navigator) =>{
- let Component = route.component;
- return <Component{...route.params} navigator = {navigator} /> //渲染场景
- }
- }
- />
- );
- }
- }
- //第一个页面
- class FirstPageComponent extends Component{
- clickJump(){
- const{navigator} = this.props;
- if(navigator){
- navigator.push({ //navigator.push 传入name和你想要跳的组件页面
- name: "SecondPageComponent",
- component: SecondPageComponent
- });
- }
- }
- render(){
- return(
- <View style = {styles.container}>
- <Text>我是第一个页面</Text>
- <TouchableHighlight
- underlayColor = "rgb(180,135,250)"
- activeOpacity = {0.5}
- style = {{
- borderRadius: 8,
- padding: 8,
- marginTop: 8,
- backgroundColor: "#F30"
- }}
- onPress = {this.clickJump.bind(this)}>
- <Text>点击进入第二个页面</Text>
- </TouchableHighlight>
- </View>
- );
- }
- }
- //第二个页面
- class SecondPageComponent extends Component{
- clickJump(){
- const{navigator} = this.props;
- if(navigator){
- navigator.pop(); //navigator.pop 使用当前页面出栈, 显示上一个栈内页面.
- }
- }
- render(){
- return(
- <View style = {styles.container}>
- <Text>我是第二个页面</Text>
- <TouchableHighlight
- underlayColor = "rgb(180, 135, 250)"
- activeOpacity = {0.5}
- style = {{
- borderRadius: 8,
- padding: 8,
- marginTop: 5,
- backgroundColor: "#F30"
- }}
- onPress = {this.clickJump.bind(this)}>
- <Text>点击返回第一个页面</Text>
- </TouchableHighlight>
- </View>
- );
- }
- }
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- justifyContent: 'center',
- alignItems: 'center',
- backgroundColor: '#FFFFFF',
- },
- });
- AppRegistry.registerComponent('navigatorProject', () => navigatorProject);
延伸:传参。
以上面的代码为基础。
一:
效果图:
- //第一个界面
- class FirstPageComponent extends Component{
- constructor(props){
- super(props);
- //参数来源
- this.state = {
- author: '华帅'
- };
- }
- clickJump(){
- const{navigator} = this.props;
- if(navigator){
- navigator.push({
- name: "SecondPageComponent",
- component: SecondPageComponent,
- //传递参数,入栈
- params: {
- author: this.state.author,
- }
- });
- }
- }
- render(){
- return(
- <View style = {styles.container}>
- <Text>我是第一个页面</Text>
- <TouchableHighlight
- underlayColor = "rgb(180, 135, 250)"
- activeOpacity = {0.5}
- style = {{
- borderRadius: 8,
- padding: 8,
- marginTop: 8,
- backgroundColor: "#F30"
- }}
- onPress = {this.clickJump.bind(this)} >
- <Text>点击进入第二个页面</Text>
- </TouchableHighlight>
- </View>
- );
- }
- }
- //第二个页面
- class SecondPageComponent extends Component{
- constructor(props) {
- super(props);
- this.state = {};
- }
- //接收传递过来的参数
- componentDidMount() {
- this.setState({
- author: this.props.author,
- });
- }
- clickJump(){
- const{navigator} = this.props;
- if(navigator){
- navigator.pop();
- }
- }
- render(){
- return(
- <View style = {styles.container}>
- <Text>我是第二个页面</Text>
- <TouchableHighlight
- underlayColor = "rgb(180, 135, 250)"
- activeOpacity = {0.5}
- style = {{
- borderRadius: 8,
- padding: 8,
- marginTop: 5,
- backgroundColor: "#F30"
- }}
- onPress = {this.clickJump.bind(this)}>
- <Text>点击返回第一个页面</Text>
- </TouchableHighlight>
- <Text>作者是:{this.state.author}</Text>
- </View>
- );
- }
- }
二:
效果图:
- //第一个页面
- class FirstPageComponent extends Component{
- constructor(props){
- super(props);
- //参数来源
- this.state = {
- author: "华帅",
- id: 1,
- user: null,
- };
- }
- clickJump(){
- const{navigator} = this.props;
- const self = this;
- if(navigator){
- navigator.push({
- name: "SecondPageComponent",
- component: SecondPageComponent,
- //传递参数,入栈
- params:{
- author: this.state.author,
- id: this.state.id,
- //从第二页获取user
- getUser: function(user){
- self.setState({
- user: user
- });
- }
- }
- });
- }
}- render(){
- if(this.state.user){
- return(
- <View>
- <Text style = {styles.container}>用户信息:{JSON.stringify(this.state.user)}</Text>
- </View>
- );
- }else{
- return(
- <View style = {styles.container}>
- <Text>我是第一个页面</Text>
- <TouchableHighlight
- underlayColor = "rgb(180, 135, 250)"
- activeOpacity = {0.5}
- style = {{
- borderRadius: 8,
- padding: 8,
- marginTop: 8,
- backgroundColor: "#F30"
- }}
- onPress = {this.clickJump.bind(this)}>
- <Text>点击进入第二个页面</Text>
- </TouchableHighlight>
- </View>
- );
- }
- }
- }
- const USER_MODELS = {
- 1: {name: '华帅', age: 44},
- 2: {name: '小明', age: 12}
- };
- //第二个页面
- class SecondPageComponent extends Component{
- constructor(props){
- super(props);
- this.state = {
- id:null
- };
- }
- //接收传递过来的参数
- componentDidMount(){
- this.setState({
- author: this.props.author,
- id: this.props.id,
- });
- }
- clickJump(){
- const{navigator} = this.props;
- if(this.props.getUser){
- let user = USER_MODELS[this.props.id];
- this.props.getUser(user);
- }
- if(navigator){
- navigator.pop();
- }
- }
- render(){
- return(
- <View style = {styles.container}>
- <Text>我是第二个页面</Text>
- <Text>ID: {this.state.id}</Text>
- <TouchableHighlight
- underlayColor = "rgb(180, 135, 250)"
- activeOpacity = {0.5}
- style = {{
- borderRadius: 8,
- padding: 8,
- marginTop: 5,
- backgroundColor: "#F30"
- }}
- onPress = {this.clickJump.bind(this)}>
- <Text>点击返回第一个页面</Text>
- </TouchableHighlight>
- <Text>作者是:{this.state.author}</Text>
- </View>
- );
- }
- }
ReactNative——页面跳转的更多相关文章
- ReactNative踩坑日志——页面跳转之——Undefined is not an Object(evaluating this2.props.navigation.navigate)
页面跳转时,报 Undefined is not an Object(evaluating this2.props.navigation.navigate) 出错原因:在一个页面组件中调用了另一个组 ...
- 混合开发的大趋势之一React Native之页面跳转
转载请注明出处:王亟亟的大牛之路 最近事情有点多,没有长时间地连贯学习,文章也停了一个多礼拜,愧疚,有时间还是继续学习,继续写! 还是先安利:https://github.com/ddwhan0123 ...
- react-native页面之间的相互传值
react-native页面之间的相互传值 之前在自己学习react-native的时候,在页面跳转和传值问题上花了一段时间去网上搜索和查找资料,自己总结了两个方法.可以参考 https://blog ...
- react-native页面间传递数据的几种方式
1. 利用react-native 事件DeviceEventEmitter 监听广播 应用场景: - 表单提交页面, A页面跳转到B页面选人, 然后返回A页面, 需要将B页面选择的数据传回A页面. ...
- ReactNavigation中如何实现页面跳转
一.ReactNavigation中如何实现页面跳转 因为每个屏幕组件(具有路由地址的组件)都是由App根组件自动创建并挂载的,App组件 在创建屏幕组件时,会自动传递进来一个props: nav ...
- JSP页面跳转的几种实现方法
使用href超链接标记 客户端跳转 使用JavaScript 客户端跳转 提交表单 客户端跳转 使用response ...
- web设计页面跳转的方法
一.asp.net c# 打开新页面或页面跳转 1. 最常用的页面跳转(原窗口被替代):Response.Redirect("newpage.aspx"); 2. 利用url地址打 ...
- 前端开发--ppt展示页面跳转逻辑实现
1. 工程地址:https://github.com/digitalClass/web_page 网站发布地址: http://115.28.30.25:8029/ 2. 今天遇到一个小问题, 同组的 ...
- Html中设置访问页面不在后进行其他页面跳转
Html中设置访问页面不在后进行其他页面跳转 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...
随机推荐
- 使用CloneDB克隆数据库
本节包含以下主题: 关于使用CloneDB克隆数据库 使用CloneDB克隆数据库 使用CloneDB克隆数据库后 关于使用CloneDB克隆数据库 出于测试目的或其他目的克隆生产数据库通常是必要的. ...
- Linux下设置时间
Linux下设置时间 提供两种最根本有效的方式,就是更改时区.这里以更改为国内上海时间例子,其他地方时区同理. 方法一 备份文件 mv /etc/localtime /etc/localtime.ba ...
- 第六章 MVC之 FileResult和JS请求二进制流文件
一.FileResult 1.简介 表示一个用于将二进制文件内容发送到响应的基类.它有三个子类: FileContentResultFilePathResultFileStreamResult 推荐阅 ...
- Java序列化Serializable
1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字节描述的过程:deserialization(反序列化)是一种将这些字节重建成一个对象的过程. 2.什么情况下需 ...
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决
记录个报错: 问题描述: 经过服务器生成图片返到前台时,在火狐浏览器中下载图片或打开图片时报错:SyntaxError: JSON.parse: unexpected character at lin ...
- linux备忘簿
1.ubuntu中按ctrl+s锁定屏幕,按ctrl+q解锁. 2.vim中撤销和恢复为u和ctlr+r 3.静态库和动态库编译命令: (1)得到hello.o g++ -c hello.cpp (2 ...
- Kendo ui 入门知识点
1. Kendo的继承 varPerson= kendo.Class.extend({...}); var person = new person(); var Parent = kendo.Clas ...
- how to avoid inheritance abuse
Liskov Principle: if S is a subtype of Type T, then any objects of type T may be repalced by objects ...
- CodeVs 1009
题意: 给出一个整数 n(n<10^30) 和 k 个变换规则(k<=15). 规则: 一位数可变换成另一个一位数: 规则的右部不能为零. 例如:n=234.有规则(k=2): 2-> ...
- Servlet随笔
HttpServlet中的getRequestURL.getRequestURI.getContextPath方法获取的字符串为 jsp文件会被编译成一个Servlet,该Servlet继承自Http ...