React-Navigation

前端架构

准备

  1. /*安装组件*/
  2. npm install --save react-navigation
  3. npm install --save react-native-gesture-handler
  4. /*添加依赖*/
  5. react-native link react-native-gesture-handler

tips

  1. 如果是通过react-cli 脚手架打包的工程可能出现安装时缺少依赖,我的根据官网上教程指导,就出现这个问题。
  2. 问题:
  3. bogon:AwesomeProject fandong$ npm install -g react-navigation
  4. npm WARN react-navigation@3.8.1 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
  5. npm WARN react-navigation@3.8.1 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
  6. npm WARN @react-navigation/native@3.4.1 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
  7. npm WARN @react-navigation/native@3.4.1 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
  8. npm WARN @react-navigation/native@3.4.1 requires a peer of react-native-gesture-handler@* but none is installed. You must install peer dependencies yourself.
  9. npm WARN react-navigation-tabs@1.1.2 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
  10. npm WARN react-navigation-tabs@1.1.2 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
  11. npm WARN react-navigation-drawer@1.2.1 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
  12. npm WARN react-navigation-drawer@1.2.1 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
  13. npm WARN react-navigation-drawer@1.2.1 requires a peer of react-native-gesture-handler@^1.0.12 but none is installed. You must install peer dependencies yourself.
  14. npm WARN @react-navigation/core@3.3.1 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
  15. npm WARN react-navigation-stack@1.3.0 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
  16. npm WARN react-navigation-stack@1.3.0 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
  17. npm WARN react-navigation-stack@1.3.0 requires a peer of react-native-gesture-handler@^1.0.0 but none is installed. You must install peer dependencies yourself.
  18. npm WARN react-native-screens@1.0.0-alpha.22 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
  19. npm WARN react-native-screens@1.0.0-alpha.22 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
  20. npm WARN react-native-safe-area-view@0.13.1 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
  21. npm WARN react-native-safe-area-view@0.13.1 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
  22. npm WARN react-native-tab-view@1.3.4 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
  23. npm WARN react-native-tab-view@1.3.4 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
  24. 提示缺少: react@*
  25. react-native@*
  26. react-native-gesture-handler@*
  27. 这可能是由于版本3.X导致的,官网也有相应的提示。
  28. 如:Since react-navigation@3.x depends on the new React.createContext API, which is added in react@16.3.x, we will require react-native@^0.54.x. Also, react-navigation@3.x needs react-native-gesture-handler to work, you will need to make sure that the version of react-native-gesture-handler you are using matches your current react-native version.
  29. 根据提示 npm install --save react@* ,等等即可。

导航API

  1. basic: 顶部导航条
  2. 1> createStackNavigator
  3. uage:
  4. createStackNavigator({
  5. Home: {
  6. screen: HomeScreen
  7. }
  8. )
  9. 参数: Home: 自定义对象,
  10. screen: 显示的对象,这里我定义了的一个HomeScreen

HomeScreen组件

  1. import React, { Component } from 'react';
  2. import {View, Text,Button, Alert} from 'react-native';
  3. import styles from '../basic/style';
  4. class HomeScreen extends Component {
  5. static navigationOptions={
  6. headerTitle:<Text>'uuu'</Text>,
  7. headerRight:(
  8. <Button
  9. onPress={()=>Alert.alert("hehe")}
  10. title="Info"
  11. color="blue"
  12. />
  13. )
  14. };
  15. componentWillMount(){
  16. //Alert.alert("Will Mount... Home");
  17. }
  18. componentWillUnmount(){
  19. //Alert.alert("Unmount Home");
  20. }
  21. render() {
  22. return (
  23. <View style={styles.container}>
  24. <Text>Home Screen</Text>
  25. <Button
  26. title="touch me"
  27. onPress={()=>this.props.navigation.navigate('Details',{
  28. id:'home1',
  29. other:'done'
  30. })}
  31. >
  32. </Button>
  33. <Button
  34. title="touch me"
  35. onPress={()=>this.props.navigation.navigate('ModalScreen',{
  36. id:'home1',
  37. other:'done'
  38. })}
  39. >
  40. </Button>
  41. </View>
  42. );
  43. }
  44. }
  45. export default HomeScreen;

导航函数

  1. React Native 开发中,每个组件props 会引入navigation 这个组件对象,常用的函数有;
  2. navigate(<componentName>,{params/*option*/})
  3. push(<componentName>,{params/*option*/})
  4. 区别: 这个函数使用都能通过this.props.navigation.navigate('Details') 到对应的界面。如果当前的界面就是Details 时,使用navigate 不在出现切换界面的效果,即不会导航。
  5. push不同,push会把这个Details 继续入栈,想下web 中,访问的网页历史记录。

传参、添加参数、去参数

  1. navigate(<componentName>,{params/*option*/})
  2. push(<componentName>,{params/*option*/})
  3. 第二参数即。
  4. 添加参数 setParam(key,value)
  5. 去参数: getParam(key,defaultValue)
  6. 第二个有个默认值需要注意下。

导航模式

  1. 默然是左右切换加载 ,第二种为 modal 即上下加载。
  2. const AppNavigator = createStackNavigator({
  3. Home:{
  4. screen: HomeScreen,
  5. navigationOptions: () => ({
  6. title: '首页'
  7. })
  8. },
  9. Details:{
  10. screen:DetailScreen,
  11. navigationOptions:({navigation})=>{
  12. return {title: navigation.getParam("id","no-id")};
  13. }
  14. },
  15. ModalScreen:{
  16. screen:ModalScreen,
  17. navigationOptions:()=>({
  18. title:'Modal'
  19. })
  20. }
  21. },{
  22. initialRouteName:"Home",
  23. mode:'modal',
  24. //headerMode:'none'
  25. });
  26. const AppContainer=createAppContainer(AppNavigator);

导航的生命周期

  1. 组件跳转当前界面A,表示A 入栈,会触发 组件的生命周期即 componentWillMount 事件触发,如果从A 切换到B,不会触发A componentWillUnMount 事件,BcomponentWillMount 触发,但是B 切换到A时,B会触发componentWillUnMount。应为A并没有出栈。

导航的样式调整

  1. /*自定义导航头*/
  2. static navigationOptions = {
  3. headerTitle: <LogoTitle />,
  4. headerRight: (
  5. <Button
  6. onPress={() => alert('This is a button!')}
  7. title="Info"
  8. color="#fff"
  9. />
  10. ),
  11. };

其他导航API

  1. createBottomTabNavigator
  2. :同第一个
  3. createDrawerNavigator
  4. usage
  5. static navigationOptions = {
  6. drawerLabel: 'Home',
  7. drawerIcon: ({ tintColor }) => (
  8. <Image
  9. source={require('./chats-icon.png')}
  10. style={[styles.icon, {tintColor: tintColor}]}
  11. />
  12. ),
  13. };
  14. createSwitchNavigator
  15. usage:
  16. const AppStack = createStackNavigator({ Home: HomeScreen, Other: OtherScreen });
  17. const AuthStack = createStackNavigator({ SignIn: SignInScreen });
  18. export default createAppContainer(createSwitchNavigator(
  19. {
  20. AuthLoading: AuthLoadingScreen,
  21. App: AppStack,
  22. Auth: AuthStack,
  23. },
  24. {
  25. initialRouteName: 'AuthLoading',
  26. }
  27. ));
  28. --
  29. AuthLoadingScreen 中调用:this.props.navigation.navigate(userToken ? 'App' : 'Auth');

Navigation 官网API地址

React-Navigation web前端架构的更多相关文章

  1. 前后端分离之Web前端架构设计

    架构设计:前后端分离之Web前端架构设计 在前面的文章里我谈到了前后端分离的一些看法,这个看法是从宏观的角度来思考的,没有具体的落地实现,今天我将延续上篇文章的主题,从纯前端的架构设计角度谈谈前后端分 ...

  2. 大型 web 前端架构设计-面向抽象编程入门

    https://mp.weixin.qq.com/s/GG6AtBz6KgNwplpaNXfggQ 大型 web 前端架构设计-面向抽象编程入门 曾探 腾讯技术工程 2021-01-04   依赖反转 ...

  3. 架构设计:前后端分离之Web前端架构设计

    在前面的文章里我谈到了前后端分离的一些看法,这个看法是从宏观的角度来思考的,没有具体的落地实现,今天我将延续上篇文章的主题,从纯前端的架构设计角度谈谈前后端分离的一种具体实现方案,该方案和我原来设想有 ...

  4. web前端架构师学习流程

    JavaScript 开发(高级) 系统知识 1.1ECMAScript标准的发展过程,ES6语言对JavaScript的改进: 1.2ES6中语法层面的新特性(let.const.参数扩展.模块化等 ...

  5. web前端知识总结

    前言: 一直想着整理一下关于前端的知识体系和资料,工作忙了些,挤挤总会有的,资料很多,就看你能不能耐下心坚持去学了,要多学多敲多想,祝你进步~ 学习之前首先要大概了解什么是HTML ,CSS , JS ...

  6. 前端架构-分层而治,铁打的MV流水的C

    大家好,我是Eluxjs的作者,Eluxjs是一套基于"微模块"和"模型驱动"的跨平台.跨框架『同构方案』,欢迎了解... 文前声明,以下推断和结论纯属个人探索 ...

  7. web前端工程师在移动互联网时代里的地位问题

    支付宝十周年推出了一个新产品:支付宝的十年账单,我也赶个时髦查看了一下我的支付宝十年账单,哎,感慨自己真是太屌丝了,不过这只是说明我使用淘宝少了,当我大规模网上购物时候,我很讨厌慢速的快递,所以我大部 ...

  8. web前端工程师在移动互联网时代里的地位问题 为啥C/S系统在PC端没有流行起来,却在移动互联网下流行了起来 为啥移动端的浏览器在很多应用里都是靠边站,人们更加倾向于先麻烦自己一下,下载安装个客户端APP

    web前端工程师在移动互联网时代里的地位问题 支付宝十周年推出了一个新产品:支付宝的十年账单,我也赶个时髦查看了一下我的支付宝十年账单,哎,感慨自己真是太屌丝了,不过这只是说明我使用淘宝少了,当我大规 ...

  9. 用“MEAN”技术栈开发web应用(一)AngularJs前端架构

    前言 不知何时突然冒出“MEAN技术栈”这个新词,听起来很牛逼的样子,其实就是我们已经熟悉了的近两年在前端比较流行的技术,mongodb.express.angularjs.nodejs,由于这几项技 ...

随机推荐

  1. SQL常用函数总结

    SQL常用函数总结 这是我在项目开发中使用db2数据库写存储过程的时候经常用到的sql函数.希望对大家有所帮助: sql cast函数 (1).CAST()函数的参数是一个表达式,它包括用AS关键字分 ...

  2. Java String、string[]、List初始化方法

    String初始化: 1.String str = new String("string1"); 2.String str = "string1"; Strin ...

  3. 单例模式的c++实现

    #pragma once #include <iostream> #include <memory> #include <Windows.h> using name ...

  4. Java 8 读取文件

    以前的Java版本中读取文件非常繁琐,现在比较简单.使用Java8的Files以及Lambda,几句代码就可以搞定. public static String getXml() { StringBuf ...

  5. mockito測試框架

    1. code package com.springinaction.knights; import static org.mockito.Mockito.*; import org.junit.Te ...

  6. BZOJ4698: Sdoi2008 Sandy的卡片(后缀数组 二分)

    题意 题目链接 Sol 不要问我为什么发两篇blog,就是为了骗访问量 后缀数组的也比较好想,先把所有位置差分,然后在height数组中二分就行了 数据好水啊 // luogu-judger-enab ...

  7. ioc autofac简单示例

    1.winform用法: nuget安装autofac public interface ILog { bool Log(string msg); } public class TXTLogger : ...

  8. Activiti 配置Oracle不能自动创建表解决方法

    使用配置文件创建工作流表 <bean id="processEngineConfiguration" class="org.activiti.engine.impl ...

  9. Windows API编程----枚举系统进程

    1.该函数可以检索系统中的每个进程的标识符(进程ID) BOOL WINAPI EnumProcesses( _Out_ DWORD *pProcessIds, _In_  DWORD cb, _Ou ...

  10. C++ 友元(系转载多人博客,添加个人见解)

    原文地址:http://blog.csdn.net/caroline_wendy/article/details/16916441 原文地址:http://www.cnblogs.com/CBDoct ...