目录html
Facebook发起的开源的一套新的APP开发方案,Facebook在当初深刻研究Hybrid开发后,以为这种模式有先天的缺陷,因此果断放弃,转而自行研究,后来推出了本身的“React Native”方案,不一样于H5,也不一样于原生Native,更像是用JS写出原生应用java
开发成本小于Native模式 Andriod-Java-Kotlin IOS-OC-Swiftreact
性能体验高于Hybridandroid
一次学习,跨平台开发Android和iOS, 小程序ios
社区繁荣git
安装脚手架react-native-cli 同时安装新的版包管理工具github
npm install -g yarn react-native-cli
shell
建立项目:doubanMovie(在不包含中文的目录执行)npm
react-native init xxx --version react-native@0.55.4
json
运行项目
打开USB调试, 配置SDK
adb devices
查看已链接设备链接模拟器: adb connect 127.0.0.1:62001
更改gradle路径doubanMovie\android\gradle\wrapper\gradle-wrapper.properties
distributionUrl
值修改成file\:///E:/Lesson/bc1/React/day03/Resource/gradle-2.14.1-all.zip
直接复制过来的路径要把反斜线\改为正斜线/在项目根目录执行react-native run-android
运行期间会开启一个Node服务,不要关闭
第一次运行报错,须要在设备上设置app的Node服务地址
解决方式: 打开app > 菜单按钮 > Dev Settings -> Debug server host ...
填写服务ip和端口号, 注意冒号用英文半角,重启服务,重启应用
React-Native与React对比
组件写法
RN提供View,Text组件,没有html的dom元素
View -> div 布局容器
Text -> p 显示文字
样式写法
使用
const styles = StyleSheet.create({...})
React-Native平台相关代码处理
const instructions = Platform.select({ ios: 'Press Cmd+R to reload,\n Cmd+D or shake for dev menu', android: 'Double tap R on your keyboard to reload,\n', });
react-native-router-flux
源码地址:https://github.com/aksonov/react-native-router-flux
应用场景:在RN项目中进行路由跳转时使用
安装方式:
yarn add react-native-router-flux
使用:
Router(路由): 通常写在项目的根组件
Stack (栈):给Scene场景提供容器
Scene(场景):设置路由跳转规则
Actions (动做):触发路由跳转
const App = () => ( <Router> <Stack key="root"> <Scene key="login" component={Login} title="Login"/> <Scene key="register" component={Register} title="Register"/> <Scene key="home" component={Home}/> </Stack> </Router> );
注意事项:
最新版的react-native-router-flux会在react-native 0.55.4版本出现isMounted(...)警告,可在App.js添加如下代码忽略警告。随后两个框架更新后,此警告也可消除。
import { YellowBox } from 'react-native' YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated'])
触发路由:三种方式 (注意导入Actions组件)
<Text onPress={Actions.movieList}>电 影</Text> <Text onPress={() => { Actions.movieList()}}>电 影</Text> <Text onPress={() => { Actions['about'].call() }}>关 于</Text>
弹性布局参考: http://www.runoob.com/w3cnote/flex-grammar.html
开源轮播图react-native-swiper
源码地址:https://github.com/leecade/react-native-swiper
应用场景:在首页展现轮播图
安装方式:
yarn add react-native-swiper
经常使用属性:
index={1} 默认位置, 从0开始 showsButtons={true} 是否显示按钮 autoplayTimeout={2.5} 自动播放停留时间 autoplay={true} 是否自动播放 showsPagination={true} 显示分页指示器
在 componentDidMount
执行请求并在回调中执行setState
// 组件已经挂载 componentDidMount() { const url = 'http://api.douban.com/v2/movie/in_theaters'; fetch(url).then(res => res.json()) .then(data => { // 处理网络json数据 this.setState({ isLoading: false, movieList: data.subjects }) // console.warn(data.subjects) }).catch((err) => { console.error(err); }); }
长列表优化
<FlatList data={this.state.movieList} keyExtractor={(item, index) => item.id} renderItem={({ item, index }) => { return ( <View style={{ padding: 10, flexDirection: 'row' }}> <Text>{item.title}: {item.year} : {index} </Text> </View> ) }} />
加载指示器
<View style={{ flex: 1, padding: 20 }}> <ActivityIndicator /> </View>
条目点击跳转
Actions.movieDetail({ "movieId": movie.id, "movieTitle": movie.title});
查看RN日志:
react-native log-ios react-native log-android
android也可在PC控制台输入
adb logcat *:S ReactNative:V ReactNativeJS:V
应用内的错误与警告
console.warn('Yellow.'); console.error('Red.');
Debug调试
在Android设备菜单中选择“Debug JS Remotely”,PC会自动经过Chrome浏览器打开调试页面 http://localhost:8081/debugger-ui (须要自行安装Chrome)。这里注意自动打开的主机地址可能不是localhost,须要手动改为localhost (不修改,则手机页面多是空白)
在Chrome浏览器按Ctrl + Shift + I
或F12
打开控制台
选中Sources选项卡 -> Network标签 -> debuggerWorker.js 打开指定组件文件便可
若是没有没有debuggerWorker.js, 查看页面最下边的Status提示。
Status: Another debugger is already connected
另外一个调试器已链接,直接使用或关闭那个调试器
Status: Waiting, press Ctrl R in simulator to reload and connect.
等待中,建议从新加载模拟器
能够在代码中打断点,Console中执行js代码打印变量、执行脚本
关闭调试:在Android设备菜单中选择“Stop Remote JS Debugging”便可
参见中文官网文档:https://reactnative.cn/docs/0.51/signed-apk-android.html#content
react-native-router-flux 路由
应用场景:在RN项目中进行路由跳转时使用
安装方式:
yarn add react-native-router-flux
react-native-swiper 开源轮播图
应用场景:在首页展现轮播图
安装方式:
yarn add react-native-swiper