目录
一、基本使用
1、创建项目
2、ReactNative的布局默认为flex布局,View标签不能直接写入文字,样式不会被继承, 单位不能加px,vh,vw等可以是百分比。
3、快速获取屏幕尺寸,Dimensions
4、StyleSheet的使用
5、Alert的使用
6、判断是哪个平台
7、常用组件解释
二、使用Mobx存放全局数据
安装依赖:
找到babel.config.js文件添加如下配置:
新建mobx目录,新建index.js文件:
组件中的使用:
动态修改数据
三、react-navigation搭建路由
1、安装使用
2、Stack导航
3、BottomTab导航
4、Drawer导航
5、MaterialTopTab导航
6、路由嵌套
7、路由传参
四、图标库的使用
1、安装依赖
2、将如下代码贴到,android/app/build.gradle文件最后,路径不能错!
3、选择你中意的图标
五、ReactNative架构
1、现有架构
2、新架构
ReactNative官方文档:
React Native 中文网 · 使用React来编写原生应用的框架
ReactNative适用于APP的一套多开,适合大型项目的开发,安装环境时注意版本对应。
一、基本使用
1、创建项目
npx react-native init myPro
2、ReactNative的布局默认为flex布局,View标签不能直接写入文字,样式不会被继承, 单位不能加px,vh,vw等可以是百分比。
3、快速获取屏幕尺寸,Dimensions
flex布局见我的博客:Flex布局总结_Dragon Wu的博客-CSDN博客_flex布局总结
案例代码:同样使用React的JSX的写法,但标签需要使用RN的标签
import React from "react"
import { View, Text, Dimensions } from "react-native"
// 获取屏幕宽高
const screenWidth = Math.round(Dimensions.get("window").width)
const screenHeight = Math.round(Dimensions.get("window").height)
/*
1 在rn中默认容器 布局方式 都是flex布局
2 方向 flex-direction:column;
3 rn中样式不会被继承
4 单位
(1)不用加px
(2)不用加vw vh
(3)可以加 百分比 ”50%“
*/
const Index = () => (
backgroundColor: "aqua",
flex: 1,
flexDirection: "row",
color: "red",
fontSize: 50,
width: "50%"
}}>
JSX
JSX
屏幕尺寸的一半
)
export default Index
效果如图。
4、StyleSheet的使用
5、Alert的使用
import React, { Component } from "react"
import { Alert, Button, View, StyleSheet } from "react-native"
class App extends Component {
createTwoButtonAlert = () => {
Alert.alert(
"警告标题",
"警告内容",
[
{
text: "取消",
onPress: () => console.log("Cancel"),
style: "cancel"
},
{
text: "确认",
onPress: () => console.log("OK"),
style: "default"
}
]
)
}
render () {
return (
发表评论