React-Navigation web前端架构
React-Navigation
前端架构
准备
/*安装组件*/
npm install --save react-navigation
npm install --save react-native-gesture-handler
/*添加依赖*/
react-native link react-native-gesture-handler
tips
如果是通过react-cli 脚手架打包的工程可能出现安装时缺少依赖,我的根据官网上教程指导,就出现这个问题。
问题:
bogon:AwesomeProject fandong$ npm install -g react-navigation
npm WARN react-navigation@3.8.1 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
npm WARN react-navigation@3.8.1 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
npm WARN @react-navigation/native@3.4.1 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
npm WARN @react-navigation/native@3.4.1 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
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.
npm WARN react-navigation-tabs@1.1.2 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
npm WARN react-navigation-tabs@1.1.2 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
npm WARN react-navigation-drawer@1.2.1 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
npm WARN react-navigation-drawer@1.2.1 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
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.
npm WARN @react-navigation/core@3.3.1 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
npm WARN react-navigation-stack@1.3.0 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
npm WARN react-navigation-stack@1.3.0 requires a peer of react-native@* but none is installed. You must install peer dependencies yourself.
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.
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.
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.
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.
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.
npm WARN react-native-tab-view@1.3.4 requires a peer of react@* but none is installed. You must install peer dependencies yourself.
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.
提示缺少: react@*
react-native@*
react-native-gesture-handler@*
这可能是由于版本3.X导致的,官网也有相应的提示。
如: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.
根据提示 npm install --save react@* ,等等即可。
导航API
basic: 顶部导航条
1> createStackNavigator
uage:
createStackNavigator({
Home: {
screen: HomeScreen
}
)
参数: Home: 自定义对象,
screen: 显示的对象,这里我定义了的一个HomeScreen
HomeScreen组件
import React, { Component } from 'react';
import {View, Text,Button, Alert} from 'react-native';
import styles from '../basic/style';
class HomeScreen extends Component {
static navigationOptions={
headerTitle:<Text>'uuu'</Text>,
headerRight:(
<Button
onPress={()=>Alert.alert("hehe")}
title="Info"
color="blue"
/>
)
};
componentWillMount(){
//Alert.alert("Will Mount... Home");
}
componentWillUnmount(){
//Alert.alert("Unmount Home");
}
render() {
return (
<View style={styles.container}>
<Text>Home Screen</Text>
<Button
title="touch me"
onPress={()=>this.props.navigation.navigate('Details',{
id:'home1',
other:'done'
})}
>
</Button>
<Button
title="touch me"
onPress={()=>this.props.navigation.navigate('ModalScreen',{
id:'home1',
other:'done'
})}
>
</Button>
</View>
);
}
}
export default HomeScreen;
导航函数
在React Native 开发中,每个组件props 会引入navigation 这个组件对象,常用的函数有;
navigate(<componentName>,{params/*option*/})
push(<componentName>,{params/*option*/})
区别: 这个函数使用都能通过this.props.navigation.navigate('Details') 到对应的界面。如果当前的界面就是Details 时,使用navigate 不在出现切换界面的效果,即不会导航。
push不同,push会把这个Details 继续入栈,想下web 中,访问的网页历史记录。
传参、添加参数、去参数
navigate(<componentName>,{params/*option*/})
push(<componentName>,{params/*option*/})
第二参数即。
添加参数 setParam(key,value)
去参数: getParam(key,defaultValue)
第二个有个默认值需要注意下。
导航模式
默然是左右切换加载 ,第二种为 modal 即上下加载。
const AppNavigator = createStackNavigator({
Home:{
screen: HomeScreen,
navigationOptions: () => ({
title: '首页'
})
},
Details:{
screen:DetailScreen,
navigationOptions:({navigation})=>{
return {title: navigation.getParam("id","no-id")};
}
},
ModalScreen:{
screen:ModalScreen,
navigationOptions:()=>({
title:'Modal'
})
}
},{
initialRouteName:"Home",
mode:'modal',
//headerMode:'none'
});
const AppContainer=createAppContainer(AppNavigator);
导航的生命周期
组件跳转当前界面A,表示A 入栈,会触发 组件的生命周期即 componentWillMount 事件触发,如果从A 切换到B,不会触发A 的componentWillUnMount 事件,B的componentWillMount 触发,但是B 切换到A时,B会触发componentWillUnMount。应为A并没有出栈。
导航的样式调整
/*自定义导航头*/
static navigationOptions = {
headerTitle: <LogoTitle />,
headerRight: (
<Button
onPress={() => alert('This is a button!')}
title="Info"
color="#fff"
/>
),
};
其他导航API
createBottomTabNavigator
:同第一个
createDrawerNavigator
usage:
static navigationOptions = {
drawerLabel: 'Home',
drawerIcon: ({ tintColor }) => (
<Image
source={require('./chats-icon.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
};
createSwitchNavigator
usage:
const AppStack = createStackNavigator({ Home: HomeScreen, Other: OtherScreen });
const AuthStack = createStackNavigator({ SignIn: SignInScreen });
export default createAppContainer(createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
App: AppStack,
Auth: AuthStack,
},
{
initialRouteName: 'AuthLoading',
}
));
--
AuthLoadingScreen 中调用:this.props.navigation.navigate(userToken ? 'App' : 'Auth');
React-Navigation web前端架构的更多相关文章
- 前后端分离之Web前端架构设计
架构设计:前后端分离之Web前端架构设计 在前面的文章里我谈到了前后端分离的一些看法,这个看法是从宏观的角度来思考的,没有具体的落地实现,今天我将延续上篇文章的主题,从纯前端的架构设计角度谈谈前后端分 ...
- 大型 web 前端架构设计-面向抽象编程入门
https://mp.weixin.qq.com/s/GG6AtBz6KgNwplpaNXfggQ 大型 web 前端架构设计-面向抽象编程入门 曾探 腾讯技术工程 2021-01-04 依赖反转 ...
- 架构设计:前后端分离之Web前端架构设计
在前面的文章里我谈到了前后端分离的一些看法,这个看法是从宏观的角度来思考的,没有具体的落地实现,今天我将延续上篇文章的主题,从纯前端的架构设计角度谈谈前后端分离的一种具体实现方案,该方案和我原来设想有 ...
- web前端架构师学习流程
JavaScript 开发(高级) 系统知识 1.1ECMAScript标准的发展过程,ES6语言对JavaScript的改进: 1.2ES6中语法层面的新特性(let.const.参数扩展.模块化等 ...
- web前端知识总结
前言: 一直想着整理一下关于前端的知识体系和资料,工作忙了些,挤挤总会有的,资料很多,就看你能不能耐下心坚持去学了,要多学多敲多想,祝你进步~ 学习之前首先要大概了解什么是HTML ,CSS , JS ...
- 前端架构-分层而治,铁打的MV流水的C
大家好,我是Eluxjs的作者,Eluxjs是一套基于"微模块"和"模型驱动"的跨平台.跨框架『同构方案』,欢迎了解... 文前声明,以下推断和结论纯属个人探索 ...
- web前端工程师在移动互联网时代里的地位问题
支付宝十周年推出了一个新产品:支付宝的十年账单,我也赶个时髦查看了一下我的支付宝十年账单,哎,感慨自己真是太屌丝了,不过这只是说明我使用淘宝少了,当我大规模网上购物时候,我很讨厌慢速的快递,所以我大部 ...
- web前端工程师在移动互联网时代里的地位问题 为啥C/S系统在PC端没有流行起来,却在移动互联网下流行了起来 为啥移动端的浏览器在很多应用里都是靠边站,人们更加倾向于先麻烦自己一下,下载安装个客户端APP
web前端工程师在移动互联网时代里的地位问题 支付宝十周年推出了一个新产品:支付宝的十年账单,我也赶个时髦查看了一下我的支付宝十年账单,哎,感慨自己真是太屌丝了,不过这只是说明我使用淘宝少了,当我大规 ...
- 用“MEAN”技术栈开发web应用(一)AngularJs前端架构
前言 不知何时突然冒出“MEAN技术栈”这个新词,听起来很牛逼的样子,其实就是我们已经熟悉了的近两年在前端比较流行的技术,mongodb.express.angularjs.nodejs,由于这几项技 ...
随机推荐
- CSS单行、多行文本溢出显示省略号(……)解决方案
单行文本溢出显示省略号(-) text-overflow:ellipsis-----部分浏览器还需要加宽度width属性 .ellipsis{ overflow: hidden; text-overf ...
- JS读取粘贴板内容
1.1 监听onpaste事件 1.1.1 定义和用法 npaste 事件在用户向元素中粘贴文本时触发. 注意: 虽然使用的 HTML 元素都支持 onpaste 事件,但实际上并非支持所有元 ...
- cf1043E. Mysterious Crime(二分 前缀和)
题意 题目链接 Sol 考场上做完前四题的时候大概还剩半个小时吧,那时候已经困的不行了. 看了看E发现好像很可做?? 又仔细看了几眼发现这不是sb题么... 先考虑两个人,假设贡献分别为\((x, y ...
- 【Udacity】解决:字幕遮挡视频内容怎么办?Udacity字幕大小调整
字幕有没有办法显示在最下方,否则把内容挡住了,根本看不清老师所指的内容 Chrome 的小插件,能方便地调节视频的字幕大小,譬如:Udacity 的学习视频字幕大小不能调节,有时候不想它占太多位置,而 ...
- Windows下Python2与Python3两个版本共存的方法详解
来源:http://www.jb51.net/article/105311.htm 这篇文章主要介绍了Windows下Python2与Python3两个版本共存的方法,文中介绍的很详细,对大家具有一定 ...
- SQL Server ->> T-SQL查询面试题之实例版
1 - 3 题: 数据表结构: OrderID ProductID OrderDate SaleAmount 1 1 2015-01-01 100 2 6 2015-02-01 900 3 1 ...
- Oracle案例04——ORA-39700: database must be opened with UPGRADE option
Oracle11.2.0.3数据库通过rman备份到Oracle11.2.0.4上做还原,报需要升级的错误,具体处理步骤如下: 一.错误信息 SQL> alter database open r ...
- 贴现力 (force of discount)
一.定义 用贴现函数a-1(t) 代替累积函数,在 t 时刻的贴现力为 增加一个负号使得贴现力为正. 二.重要的公式
- yii2框架安装运行init.bat报错php.exe不是内部或外部命令
在安装yii2框架的时候,遇到一个很纠结的问题.就是当我把安装包下载下来之后,在公司的电脑安装可以正常,当我回家用自己的电脑安装就报错,提示 php.exe 不是内部或外部命令,也不是可运行的程序.这 ...
- 稳定sqlplan方法
参考文档:SQLT (SQLTXPLAIN) - Tool that helps to diagnose SQL statements performing poorly [ID 215187.1]