效果图:

进入工作目录,运行

react-native init NavigatorProject

创建项目NavigatorProject

  1. import React, { Component } from 'react';
  2. import {
  3. AppRegistry,
  4. StyleSheet,
  5. Text,
  6. View,
  7. TouchableHighlight,
  8. Image,
  9. Navigator
  10. } from 'react-native';
  1. class navigatorProject extends Component{
  2.     render(){
  3.         let defaultName = 'firstPageName';
  4.         let defaultComponent = FirstPageComponent;
  5.         return(
  6.             <Navigator
  7.                 initialRoute = {{name: defaultName, component: defaultComponent}}  //初始化导航器Navigator,指定默认的页面
  8.                 configureScene = {
  9.                     (route) => {
  10.                         return Navigator.SceneConfigs.FloatFromRight;  //配置场景动画,页面之间跳转时候的动画
  11.                     }
  12.                 }
  13.                 renderScene = {
  14.                     (route, navigator) =>{
  15.                         let Component = route.component;
  16.                         return <Component{...route.params} navigator = {navigator} />  //渲染场景
  17.                     }
  18.                 }
  19.             />
  20.         );
  21.     }
  22. }
  1. //第一个页面
  2.  
  3. class FirstPageComponent extends Component{
  4.     clickJump(){
  5.         const{navigator} = this.props;
  6.         if(navigator){
  7.             navigator.push({  //navigator.push 传入name和你想要跳的组件页面
  8.                 name: "SecondPageComponent",
  9.                 component: SecondPageComponent
  10.             });
  11.         }
  12.     }
  13.  
  14.     render(){
  15.         return(
  16.             <View style = {styles.container}>
  17.                 <Text>我是第一个页面</Text>
  18.                 <TouchableHighlight
  19.                     underlayColor = "rgb(180,135,250)"
  20.                     activeOpacity = {0.5}
  21.                     style = {{
  22.                         borderRadius: 8,
  23.                         padding: 8,
  24.                         marginTop: 8,
  25.                         backgroundColor: "#F30"
  26.                     }}
  27.                     onPress = {this.clickJump.bind(this)}>
  28.                     <Text>点击进入第二个页面</Text>
  29.                 </TouchableHighlight>
  30.             </View>
  31.         );
  32.     }
  33. }
  1. //第二个页面
  2.  
  3. class SecondPageComponent extends Component{
  4.     clickJump(){
  5.         const{navigator} = this.props;
  6.         if(navigator){
  7.             navigator.pop();  //navigator.pop 使用当前页面出栈, 显示上一个栈内页面.
  8.         }
  9.     }
  10.  
  11.     render(){
  12.         return(
  13.             <View style = {styles.container}>
  14.                 <Text>我是第二个页面</Text>
  15.                 <TouchableHighlight
  16.                     underlayColor = "rgb(180, 135, 250)"
  17.                     activeOpacity = {0.5}
  18.                     style = {{
  19.                         borderRadius: 8,
  20.                         padding: 8,
  21.                         marginTop: 5,
  22.                         backgroundColor: "#F30"
  23.                     }}
  24.                     onPress = {this.clickJump.bind(this)}>
  25.                     <Text>点击返回第一个页面</Text>
  26.                 </TouchableHighlight>
  27.             </View>
  28.         );
  29.     }
  30. }
  1. const styles = StyleSheet.create({
  2.  container: {
  3.   flex: 1,
  4.   justifyContent: 'center',
  5.   alignItems: 'center',
  6.   backgroundColor: '#FFFFFF',
  7.  },
  8. });
  9.  
  10. AppRegistry.registerComponent('navigatorProject', () => navigatorProject);

延伸:传参。

以上面的代码为基础。

一:

效果图:

  1. //第一个界面
  2. class FirstPageComponent extends Component{
  3. constructor(props){
  4. super(props);
  5. //参数来源
  6. this.state = {
  7. author: '华帅'
  8. };
  9. }
  10.  
  11. clickJump(){
  12. const{navigator} = this.props;
  13. if(navigator){
  14. navigator.push({
  15. name: "SecondPageComponent",
  16. component: SecondPageComponent,
  17. //传递参数,入栈
  18. params: {
  19. author: this.state.author,
  20. }
  21. });
  22. }
  23. }
  24.  
  25. render(){
  26. return(
  27. <View style = {styles.container}>
  28. <Text>我是第一个页面</Text>
  29. <TouchableHighlight
  30. underlayColor = "rgb(180, 135, 250)"
  31. activeOpacity = {0.5}
  32. style = {{
  33. borderRadius: 8,
  34. padding: 8,
  35. marginTop: 8,
  36. backgroundColor: "#F30"
  37. }}
  38. onPress = {this.clickJump.bind(this)} >
  39. <Text>点击进入第二个页面</Text>
  40. </TouchableHighlight>
  41. </View>
  42. );
  43. }
  44. }
  1. //第二个页面
  2. class SecondPageComponent extends Component{
  3. constructor(props) {
  4. super(props);
  5. this.state = {};
  6. }
  7.  
  8. //接收传递过来的参数
  9. componentDidMount() {
  10. this.setState({
  11. author: this.props.author,
  12. });
  13. }
  14.  
  15. clickJump(){
  16. const{navigator} = this.props;
  17. if(navigator){
  18. navigator.pop();
  19. }
  20. }
  21.  
  22. render(){
  23. return(
  24. <View style = {styles.container}>
  25. <Text>我是第二个页面</Text>
  26. <TouchableHighlight
  27. underlayColor = "rgb(180, 135, 250)"
  28. activeOpacity = {0.5}
  29. style = {{
  30. borderRadius: 8,
  31. padding: 8,
  32. marginTop: 5,
  33. backgroundColor: "#F30"
  34. }}
  35. onPress = {this.clickJump.bind(this)}>
  36. <Text>点击返回第一个页面</Text>
  37. </TouchableHighlight>
  38. <Text>作者是:{this.state.author}</Text>
  39. </View>
  40. );
  41. }
  42. }

二:

效果图:

  1. //第一个页面
  2.  
  3. class FirstPageComponent extends Component{
  4.  
  5. constructor(props){
  6. super(props);
  7. //参数来源
  8. this.state = {
  9. author: "华帅",
  10. id: 1,
  11. user: null,
  12. };
  13. }
  14.  
  15. clickJump(){
  16. const{navigator} = this.props;
  17. const self = this;
  18. if(navigator){
  19. navigator.push({
  20. name: "SecondPageComponent",
  21. component: SecondPageComponent,
  22. //传递参数,入栈
  23. params:{
  24. author: this.state.author,
  25. id: this.state.id,
  26. //从第二页获取user
  27. getUser: function(user){
  28. self.setState({
  29. user: user
  30. });
  31. }
  32. }
  33. });
  34. }
        }
  35.  
  36. render(){
  37. if(this.state.user){
  38. return(
  39. <View>
  40. <Text style = {styles.container}>用户信息:{JSON.stringify(this.state.user)}</Text>
  41. </View>
  42. );
  43. }else{
  44. return(
  45. <View style = {styles.container}>
  46. <Text>我是第一个页面</Text>
  47. <TouchableHighlight
  48. underlayColor = "rgb(180, 135, 250)"
  49. activeOpacity = {0.5}
  50. style = {{
  51. borderRadius: 8,
  52. padding: 8,
  53. marginTop: 8,
  54. backgroundColor: "#F30"
  55. }}
  56. onPress = {this.clickJump.bind(this)}>
  57. <Text>点击进入第二个页面</Text>
  58. </TouchableHighlight>
  59. </View>
  60. );
  61. }
  62. }
  63. }
  1. const USER_MODELS = {
  2. 1: {name: '华帅', age: 44},
  3. 2: {name: '小明', age: 12}
  4. };
  1. //第二个页面
  2.  
  3. class SecondPageComponent extends Component{
  4. constructor(props){
  5. super(props);
  6. this.state = {
  7. id:null
  8. };
  9. }
  10.  
  11. //接收传递过来的参数
  12. componentDidMount(){
  13. this.setState({
  14. author: this.props.author,
  15. id: this.props.id,
  16. });
  17. }
  18.  
  19. clickJump(){
  20. const{navigator} = this.props;
  21. if(this.props.getUser){
  22. let user = USER_MODELS[this.props.id];
  23. this.props.getUser(user);
  24. }
  25.  
  26. if(navigator){
  27. navigator.pop();
  28. }
  29. }
  30.  
  31. render(){
  32. return(
  33. <View style = {styles.container}>
  34. <Text>我是第二个页面</Text>
  35. <Text>ID: {this.state.id}</Text>
  36. <TouchableHighlight
  37. underlayColor = "rgb(180, 135, 250)"
  38. activeOpacity = {0.5}
  39. style = {{
  40. borderRadius: 8,
  41. padding: 8,
  42. marginTop: 5,
  43. backgroundColor: "#F30"
  44. }}
  45. onPress = {this.clickJump.bind(this)}>
  46. <Text>点击返回第一个页面</Text>
  47. </TouchableHighlight>
  48. <Text>作者是:{this.state.author}</Text>
  49. </View>
  50. );
  51. }
  52. }

ReactNative——页面跳转的更多相关文章

  1. ReactNative踩坑日志——页面跳转之——Undefined is not an Object(evaluating this2.props.navigation.navigate)

    页面跳转时,报  Undefined is not an Object(evaluating this2.props.navigation.navigate) 出错原因:在一个页面组件中调用了另一个组 ...

  2. 混合开发的大趋势之一React Native之页面跳转

    转载请注明出处:王亟亟的大牛之路 最近事情有点多,没有长时间地连贯学习,文章也停了一个多礼拜,愧疚,有时间还是继续学习,继续写! 还是先安利:https://github.com/ddwhan0123 ...

  3. react-native页面之间的相互传值

    react-native页面之间的相互传值 之前在自己学习react-native的时候,在页面跳转和传值问题上花了一段时间去网上搜索和查找资料,自己总结了两个方法.可以参考 https://blog ...

  4. react-native页面间传递数据的几种方式

    1. 利用react-native 事件DeviceEventEmitter 监听广播 应用场景: - 表单提交页面, A页面跳转到B页面选人, 然后返回A页面, 需要将B页面选择的数据传回A页面. ...

  5. ReactNavigation中如何实现页面跳转

    一.ReactNavigation中如何实现页面跳转 因为每个屏幕组件(具有路由地址的组件)都是由App根组件自动创建并挂载的,App组件 在创建屏幕组件时,会自动传递进来一个props:   nav ...

  6. JSP页面跳转的几种实现方法

    使用href超链接标记      客户端跳转 使用JavaScript               客户端跳转 提交表单                        客户端跳转 使用response ...

  7. web设计页面跳转的方法

    一.asp.net c# 打开新页面或页面跳转 1. 最常用的页面跳转(原窗口被替代):Response.Redirect("newpage.aspx"); 2. 利用url地址打 ...

  8. 前端开发--ppt展示页面跳转逻辑实现

    1. 工程地址:https://github.com/digitalClass/web_page 网站发布地址: http://115.28.30.25:8029/ 2. 今天遇到一个小问题, 同组的 ...

  9. Html中设置访问页面不在后进行其他页面跳转

    Html中设置访问页面不在后进行其他页面跳转 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

随机推荐

  1. 使用CloneDB克隆数据库

    本节包含以下主题: 关于使用CloneDB克隆数据库 使用CloneDB克隆数据库 使用CloneDB克隆数据库后 关于使用CloneDB克隆数据库 出于测试目的或其他目的克隆生产数据库通常是必要的. ...

  2. Linux下设置时间

    Linux下设置时间 提供两种最根本有效的方式,就是更改时区.这里以更改为国内上海时间例子,其他地方时区同理. 方法一 备份文件 mv /etc/localtime /etc/localtime.ba ...

  3. 第六章 MVC之 FileResult和JS请求二进制流文件

    一.FileResult 1.简介 表示一个用于将二进制文件内容发送到响应的基类.它有三个子类: FileContentResultFilePathResultFileStreamResult 推荐阅 ...

  4. Java序列化Serializable

    1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字节描述的过程:deserialization(反序列化)是一种将这些字节重建成一个对象的过程. 2.什么情况下需 ...

  5. SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决

    记录个报错: 问题描述: 经过服务器生成图片返到前台时,在火狐浏览器中下载图片或打开图片时报错:SyntaxError: JSON.parse: unexpected character at lin ...

  6. linux备忘簿

    1.ubuntu中按ctrl+s锁定屏幕,按ctrl+q解锁. 2.vim中撤销和恢复为u和ctlr+r 3.静态库和动态库编译命令: (1)得到hello.o g++ -c hello.cpp (2 ...

  7. Kendo ui 入门知识点

    1. Kendo的继承 varPerson= kendo.Class.extend({...}); var person = new person(); var Parent = kendo.Clas ...

  8. 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 ...

  9. CodeVs 1009

    题意: 给出一个整数 n(n<10^30) 和 k 个变换规则(k<=15). 规则: 一位数可变换成另一个一位数: 规则的右部不能为零. 例如:n=234.有规则(k=2): 2-> ...

  10. Servlet随笔

    HttpServlet中的getRequestURL.getRequestURI.getContextPath方法获取的字符串为 jsp文件会被编译成一个Servlet,该Servlet继承自Http ...