by Amanda Bullington

通过阿曼达·布林顿(Amanda Bullington)

我在React Native中构建时获得的经验教训 (Lessons I learned while building in React Native)

When I received an offer for a software engineering role to build an app in React Native, I wasn’t sure what to expect.

当我收到要在React Native中构建应用程序的软件工程职位的邀请时,我不确定会发生什么。

On one hand, it sounded exciting to be able to build a mobile application for iOS and Android using a single codebase. On the other, hearing that companies like Airbnb had tested the platform and ultimately decided against it left me feeling like there would be a fair number of challenges ahead.

一方面,能够使用单个代码库为iOS和Android构建移动应用程序听起来很令人兴奋。 另一方面,听说像Airbnb这样的公司已经对该平台进行了测试,并最终决定反对该平台,这让我感到未来将面临许多挑战。

Now I’m a few months in, and here are some of the lessons I learned along the way.

现在我已经有几个月了,这是我在此过程中学到的一些经验教训。

选择合适的库 (Choosing the right libraries)

One of the first things I learned about React Native is that the choice of third-party libraries is often limited. As a JavaScript web developer, I had a wide choice of libraries that I could customize to various projects.

我从React Native中学到的第一件事是,第三方库的选择通常受到限制。 作为一名JavaScript Web开发人员,我可以选择多种库,可以针对各种项目进行自定义。

React Native libraries are more complex to build. They require a knowledge of native code for iOS and Android to work across platforms. Because of this, there aren’t as many people who develop libraries for React Native.

React Native库的构建更加复杂。 他们需要iOS和Android的本机代码知识才能跨平台工作。 因此,为React Native开发库的人不多。

After some futile searching on GitHub, I ended up choosing most of my app libraries from the React Native Community repo. These are generally the best maintained and almost guaranteed to work with the latest version of React. The Native Directory was another helpful place to quickly search what’s available in React Native.

在GitHub上进行了徒劳的搜索之后,我最终从React Native Community repo中选择了我的大多数应用程序库。 这些通常是维护得最好的,几乎可以保证可以与最新版本的React一起使用。 Native Directory是另一个快速搜索React Native中可用内容的有用位置。

Even within the RN community repo, not all libraries worked out of the box. Sometimes I needed to fork the repo and make a few tweaks of my own. Other times, I needed to downgrade to a version that fixed the particular bug that showed up in my app. Version control is all the more important when there are few libraries and few maintainers.

即使在RN社区回购库中,并非所有库都可以立即使用。 有时我需要分叉仓库,并自己做一些调整。 其他时候,我需要降级到可修复出现在我的应用程序中的特定错误的版本。 当库和维护者很少时,版本控制就显得尤为重要。

适应Flexbox (Getting comfortable with Flexbox)

With more than 10,000 types of devices for Android alone, it can be tricky to build an app that works for all screen sizes. I needed my app to look good on devices as small as the iPhone SE and as large as the Pixel 2XL.

仅使用10,000多种类型的Android设备,就很难构建适用于所有屏幕尺寸的应用。 我需要我的应用在小至iPhone SE和大至Pixel 2XL的设备上看起来都不错。

At first, I tried styling my app by using React Native’s built-in Dimensions class to find the width and height of each screen. Ultimately, this was too complicated to maintain as the app grew. Instead, Flexbox is the key to being able to tackle styling across screen sizes gracefully. A quick run through of the Flexbox Froggy tool is a good way to get up to speed.

最初,我尝试使用React Native的内置Dimensions类来设计应用程序的样式,以查找每个屏幕的宽度和高度。 最终,随着应用程序的增长,这太复杂了,难以维护。 相反,Flexbox是能够优雅地处理各种屏幕尺寸样式的关键。 快速运行Flexbox Froggy工具是加快速度的好方法。

Flexbox didn’t solve all my styling problems. I still encountered quirky screen sizes that needed their own styling solutions like the SafeAreaView for the iPhone X series. I also needed to use conditional statements for different iOS and Android styling on many screens. But overall, it’s a great tool for designing apps in React Native.

Flexbox并不能解决我所有的样式问题。 我仍然遇到奇怪的屏幕尺寸,需要他们自己的样式解决方案,例如iPhone X系列的SafeAreaView 。 我还需要在许多屏幕上针对不同的iOS和Android样式使用条件语句。 但总的来说,它是在React Native中设计应用程序的绝佳工具。

将其关闭然后再次打开 (Turning it off and back on again)

Once I installed a new third-party library and ran react-native link, I often ran into the “undefined is not an object” error. React Native is known for its non-descriptive error messages. It took me a while to figure out what this meant. At first, I thought there was something wrong with the library. Or that it didn’t work with the version of React Native I had installed.

安装新的第三方库并运行react-native link ,我经常会遇到“未定义不是对象”错误。 React Native以其非描述性错误消息而闻名。 我花了一段时间才弄清楚这是什么意思。 起初,我认为图书馆有问题。 或者它与我安装的React Native版本不兼容。

Then, deep in a GitHub issue thread for one particular library, I found this comment that finally shed light on why none of my libraries were working smoothly.

然后,深入到某个特定库的GitHub问题线程中,我发现此评论最终阐明了为什么我的所有库都无法正常工作。

Like many developers, I had gotten into the habit of simply reloading my project while running react-native run-android or react-native run-ios. Hot-reloading is great for saving time while making tiny styling tweaks to the app and checking out screens. However, it does not help integrate new libraries into the app. My new libraries wouldn’t work until I closed all my simulators/emulators, disconnected my devices, and re-ran npm start to restart the Metro bundler.

像许多开发人员一样,我养成了在运行react-native run-androidreact-native run-ios时仅重新加载项目的习惯。 热重装非常适合节省时间,同时对应用程序进行细微的样式调整并签出屏幕。 但是,它无助于将新库集成到应用程序中。 在关闭所有模拟器/仿真器,断开设备连接并重新运行npm start以重新启动Metro捆绑程序之前,我的新库将无法工作。

In other words, I needed to turn everything off and back on again to smoothly integrate third-party libraries without misleading error messages.

换句话说,我需要关闭然后重新打开所有内容,以流畅地集成第三方库而不会误导错误消息。

在没有调试器的情况下工作 (Working without a debugger)

As a web developer, I was practiced at searching for bugs in the Google Chrome debugger. In React Native, it only took a few weeks before I lost my ability to debug in Chrome.

作为一名网络开发人员,我曾练习过在Google Chrome调试器中搜索错误。 在React Native中,只花了几周的时间,我就失去了在Chrome中进行调试的能力。

One of the constraints of my app was that I needed to use Realm as my primary database. However, Realm has a frequently reported issue where it cannibalizes the Chrome debugger, making it impossible to use. I needed to find a different solution.

我的应用程序的限制之一是我需要使用Realm作为主数据库。 但是,Realm有一个经常报道的问题 ,它蚕食了Chrome调试器,使其无法使用。 我需要找到其他解决方案。

React Native has a built-in debugger where you can log console.logs to the terminal with react-native log-android or react-native log-ios. While this works well on Android, I ran into issues using this debugger for iOS. I began to adopt an Android-first approach to development, where I would build and test everything on Android to easily access console.logs, then make tweaks to the iOS version as needed. I also invested in writing better error messages within my app, which benefited both my users and me.

React Native有一个内置的调试器 ,您可以使用react-native log-androidreact-native log-ios console.logs登录到终端。 虽然这在Android上运行良好,但使用iOS调试器时遇到了问题 。 我开始采用Android优先的开发方法,在该方法中,我将在Android上构建和测试所有内容以轻松访问console.logs,然后根据需要对iOS版本进行调整。 我还投资于在应用程序中编写更好的错误消息,这对我和我的用户都有利。

I also experimented with using XCode and Android Studio to debug, but I ultimately found my Android-first approach to be the easiest solution with the least amount of screen switching.

我也尝试过使用XCode和Android Studio进行调试,但是最终我发现以Android为先的方法是最简单的解决方案,并且屏幕切换次数最少。

尽早进行生产构建 (Running production builds early)

Experienced React Native developers have told me that they rarely run into any issues in production mode that they didn’t already see and solve for in development. That wasn’t my experience. When I ran my production builds on physical devices, I was able to catch a few errors that I hadn’t spotted before.

经验丰富的React Native开发人员告诉我,他们很少在生产模式中遇到他们尚未在开发中看到并解决的问题。 那不是我的经验。 当我在物理设备上运行产品时,我能够捕捉到一些以前未发现的错误。

One example was navigation. Setting up navigation in a mobile app was tricky for me to wrap my head around at first, and I needed to make a few changes to the way I set up my react-navigation library to deliver data to the user at the right time. Using a physical device let me simulate all the ways a user might run through my app (i.e. when they would move to a new screen vs. pressing the back button) and set up navigation accordingly.

一个例子是导航。 首先,在移动应用程序中设置导航对我来说很棘手,我需要对设置React导航库的方式进行一些更改,以便在正确的时间将数据传递给用户。 使用物理设备,我可以模拟用户可能会通过我的应用程序运行的所有方式(即,当他们移动到新屏幕而不是按下后退按钮时)并相应地设置导航。

Another issue that I found in production involved dangerous Android permissions. Newer Android phones require more explicit permission requests, and once I tested on a physical device, I realized that my app’s photo gallery needed these permissions to load correctly.

我在生产中发现的另一个问题涉及危险的Android权限 。 较新的Android手机需要更明确的权限请求,并且在物理设备上进行测试后,我意识到我的应用程序的图片库需要这些权限才能正确加载。

结论 (Conclusion)

React Native is well-documented and relatively quick to learn, especially if you already know React. It’s immensely satisfying to build a mobile application that works across both iOS and Android with a single codebase.

React Native的文档资料丰富,而且学习起来相对较快,特别是如果您已经知道React。 使用单个代码库构建可同时在iOS和Android上运行的移动应用程序非常令人满足。

The challenges I ran into above were some of the trickier parts — but overall, there weren’t any huge hurdles to developing an app in React Native. Mostly, I needed to wrap my head around the quirks of mobile development and get comfortable with some of the awkward error messages. Now that I’m past these initial learning curves and settled on an Android-first approach, development is much faster.

我在上面遇到的挑战是一些棘手的部分-但总的来说,在React Native中开发应用程序并没有很大的障碍。 通常,我需要全神贯注于移动开发的怪癖,并适应一些尴尬的错误消息。 现在,我已经超越了这些最初的学习曲线,并选择了Android优先的方法,开发速度将大大提高。

Would I develop in React Native again? Absolutely.

我会再次在React Native中进行开发吗? 绝对。

翻译自: https://www.freecodecamp.org/news/lessons-i-learned-while-building-in-react-native-917cb7bb5993/

我在React Native中构建时获得的经验教训相关推荐

  1. 如何在React Native中构建项目并管理静态资源

    by Khoa Pham 通过Khoa Pham 如何在React Native中构建项目并管理静态资源 (How to structure your project and manage stati ...

  2. 如何在React Native中创建精美的动画加载器

    by Vikrant Negi 通过Vikrant Negi 如何在React Native中创建精美的动画加载器 (How to create a beautifully animated load ...

  3. Taro3.2 适配 React Native 之运行时架构详解

    导读 由 58 前端团队主导的 Taro 3 适配 React Native 工作已完成有一段时间了.目前发布了多个体验版,也将在3月底迎来正式版.基于 Taro 的良好架构演变,适配 React N ...

  4. React Native 中的 Android 原生模块

    当使用 React Native 开发 Android 应用时,你可能需要使用没有被 React Native 封装的模块.但你可以使用 Java 编写原生模块,然后选择性的暴露公共接口到 React ...

  5. 理解 React Native 中的 AJAX 请求

    曾经,大多数 Web 应用程序通过用户操作刷新整个网页以与 Web 服务器通信. 后来,AJAX(异步 JavaScript 和 XML)概念通过提供一种在后台与 Web 服务器通信的方式使 Web ...

  6. 在 React Native 中实现 3D 动画

    本文的范围将涵盖对 Three.js 库和 Animated API 的探索. 您应该具备 JavaScript 和 React Native 的基本知识才能继续学习: 要了解更多关于可以在 Reac ...

  7. 解决 React Native 中的常见错误

    React Native 是当今最常用的 JavaScript 移动框架之一.React Native 使熟悉 JavaScript 和 React Web 框架的开发人员能够使用类似的方法和原理开发 ...

  8. 如何在React Native中写一个自定义模块

    前言 在 React Native 项目中可以看到 node_modules 文件夹,这是存放 node 模块的地方,Node.js 的包管理器 npm 是全球最大的开源库生态系统.提到npm,一般指 ...

  9. 如何在React Native中记录日志?

    本文翻译自:How to do logging in React Native? 如何为Web开发时在React Native中记录变量,例如使用console.log ? #1楼 参考:https: ...

最新文章

  1. 2022-2028年中国橡胶漆产业发展动态及未来趋势预测报告
  2. sql语句技巧,不敢独享,特此呈上
  3. Liferay7 BPM门户开发之5: Activiti和Spring集成
  4. 【数据挖掘】数据挖掘算法 组件化思想 ( 模型或模式结构 | 数据挖掘任务 | 评分函数 | 搜索和优化算法 | 数据管理策略 )
  5. 点击更新没反应_Edge浏览器双击无反应?再也不用烦恼啦
  6. 百度搜索与推荐引擎的云原生改造
  7. linux启动本地远程服务,如何使用SSH在本地控制远程服务器执行命令
  8. 面试思考,入职初期怎么做
  9. Windows Server 2012搭建文件服务器
  10. 用计算机看手机照片大小,手机怎么知道照片多少k
  11. 迷你聊天室_简介:聊天机器人与我们的第一个迷你课程
  12. 如何在互联网上寻找免费电子书?(其二)
  13. 计算机应用 行动计划范文,制定计算机学习计划范文3篇0001.docx
  14. HDU - 1546 Idiomatic Phrases Game(dijkstra最短路)
  15. python 一个简单的网站采集
  16. 附录:入行 AI,选个脚踏实地的岗位
  17. Weather API 天气应用 API调用分享
  18. 向量的加减(输出重载)
  19. CSS清除浮动 清除float浮动
  20. java计算机毕业设计个人连锁民宿信息管理系统设计与开发系统(修改)源码+mysql数据库+系统+lw文档+部署

热门文章

  1. 1小时学会:最简单的iOS直播推流(八)h264/aac 软编码
  2. js获取前后几天或者前后几个月的日期
  3. PHP入门 1 phpstudy安装与配置站点
  4. 【Runtime】动态添加方法demo
  5. [20190402]对比_mutex_wait_scheme不同模式cpu消耗.txt
  6. 十分钟成为 Contributor 系列 | 为 TiDB 重构 built-in 函数
  7. 使用let替换var实现块级作用域的小发现
  8. python中的文档字符串(docString)
  9. Visual Studio中的《C# 语言规范》
  10. App Store 审核被拒整理