這是我最喜歡的使用React Native創建生產級應用程序的技巧

Trust me when I say this, React Native is hard. And it’s not the usual hard of what we think hard is. It is hard in terms of working with in general. In this blog post, I’ll go over some tips and tricks and eventually the best practices I’ve deployed for one of my apps coded in React Native: codedamn Android (or codedamn iOS).

當我這樣說時請相信我,React Native很難。 而且,這并不是我們很難想到的。 從總體上講,這很難。 在這篇博客文章中,我將介紹一些技巧和竅門,以及最終為在React Native中編碼的我的一個應用程序部署的最佳實踐: codedamn Android (或codedamn iOS )。

Hi! My name is Mehul. I’m a student, youtuber, fullstack developer, app developer and can deploy servers as well. Recently, I decided to launch a developer focused platform (you guessed it right, codedamn). To get it off the ground real quick on mobile devices, I went with React Native. Partly because I’m not a huge fan of Swift and Xcode for now. But little did I know that I’d be interacting more with native code than I thought. Anyway, let’s begin with the information I want to mention.

嗨! 我叫Mehul。 我是一名學生, youtuber ,全棧開發人員 ,應用程序開發人員,也可以部署服務器。 最近,我決定啟動一個以開發人員為中心的平臺(代碼猜錯了,您猜對了)。 為了在移動設備上快速真正地投入使用,我選擇了React Native。 部分原因是我暫時不喜歡Swift和Xcode。 但是我幾乎不知道我會與本機代碼進行比我想像的更多的交互。 無論如何,讓我們從我要提及的信息開始。

Note: At the time of writing this article, React Native was at v0.57-rc4. Check if some of the things are already available/fixed in recent React Native version!

注意:在撰寫本文時,React Native的版本為v0.57-rc4。 檢查某些事情在最新的React Native版本中是否已經可用/已修復!

0:知道你在做什么 (0: Know what you’re doing)

Realize that the React Native world is a lonely world right now. You might get yourself into a problem which no one has faced till now (or you’re not able to google it properly). Always keep VCS up with your native project and regularly commit your changes (all the cool kids call it CI). It helps you to revert back to the last working copy pretty quickly without losing a lot of code.

意識到React Native世界現在是一個孤獨的世界。 您可能會陷入一個迄今為止沒有人遇到過的問題(或者您無法正確搜索該問題)。 始終使VCS與您的本機項目保持一致,并定期提交更改(所有很棒的孩子都將其稱為CI)。 它可以幫助您快速恢復到上一個??工作副本,而不會丟失很多代碼。

It is equally important to know what you’re doing. You might end up breaking your project completely if you’re unaware. If you did not use a VCS, well then you are in trouble.

知道自己在做什么同樣重要。 如果您不知道,您可能最終會完全破壞您的項目。 如果您沒有使用VCS,那么您將遇到麻煩。

1:升級您的JSC (1: Upgrade your JSC)

JSC (JavaScriptCore) is a webkit based JavaScript engine used by React Native on Android platforms to evaluate your JavaScript code. Don’t tell me you thought that React Native converts JavaScript into native code. It doesn’t! ;-)

JSC(JavaScriptCore)是基于WebkitJavaScript引擎,React Native在Android平臺上使用它來評估您JavaScript代碼。 不要告訴我您認為React Native將JavaScript轉換為本地代碼。 不是! ;-)

Whatever JS you write is still executed as JavaScript only by JSC on Android. The problem is React Native ships with a very old version of JSC. This means you have to use babel transformations extensively. Sometimes there are bugs so nasty you’ll pull your hair every time you sit to code, because of an older version of JSC.

無論您編寫什么JS,仍然只能由Android上的JSC作為JavaScript執行。 問題是React Native附帶了非常舊的JSC版本。 這意味著您必須廣泛使用babel轉換。 有時,由于JSC的版本較舊,您有時每次都坐下來編寫代碼時會感到非常討厭,以至于無法忍受。

I learned it the hard way after wasting a day of debugging. There was an unknown random fatal error in between app execution. After studying the logs for quite some time, I came to a conclusion that the app was crashing somewhere where [Symbol.iterator] is used in the transpiled JS code by babel.

在浪費了一天的調試時間后,我很難學了。 應用執行之間存在未知的隨機致命錯誤。 在研究日志一段時間后,我得出一個結論,該應用程序崩潰了,Babel在轉譯的JS代碼中使用了[Symbol.iterator]。

Now, Symbols is an ES6 thing. Babel did not transpile this further, and JSC was so old it wasn’t able to hold up simple things like these and crashed. I wasted almost a day behind figuring out that JSC upgrade was a better solution than other patchy hacks.

現在,Symbols是ES6。 Babel并沒有進一步傳播它,而JSC太老了,以至于無法忍受像這樣的簡單事情而崩潰了。 我花了將近一天的時間弄清楚JSC升級是比其他補丁程序更好的解決方案。

Upgrading your JSC is pretty straightforward. Follow this github repo and you should be up and running in no time.

升級JSC非常簡單。 按照此github存儲庫操作 ,您應該立即啟動并運行。

2:正確設置Redux (2: Setup Redux correctly)

Redux can be a pain to setup correctly. And by setting it up correctly, I mean integrating it deeply with your app. Whether it is your own reducers or whether it is React navigation. Setting up react navigation with Redux is a great decision for the long-term even though the React navigation page gives a warning about this:

Redux可能難以正確設置。 通過正確設置,我的意思是將其與您的應用程序深度集成。 無論是您自己的減速器還是React導航。 從長期來看,使用Redux設置React導航是一個不錯的選擇,即使React導航頁面對此提供警告:

Heck no. We’re talking about enterprise and production level apps here. Go ahead and store your navigation state in Redux and gain very fine control over your state.

哎呀 我們在這里談論企業和生產級應用程序。 繼續并將您的導航狀態存儲在Redux中,并可以很好地控制您的狀態。

But remember, with great power comes great responsibility. With such fine control over your navigation, make sure you set it up correctly. Or else your app will randomly crash. It’s going to be a pain to set it up initially, but trust me its worth the time.

但是請記住,強大的力量伴隨著巨大的責任。 通過對導航的這種精細控制,請確保正確設置了它。 否則您的應用將隨機崩潰。 最初設置它會很痛苦,但是請相信我,值得您花時間。

Read about Redux and its integration with react navigation here.

在此處閱讀有關Redux及其與react導航的集成的信息 。

3:使用可用的自動化工具,例如fastlane (3: Use available automation tools like fastlane)

Fastlane is a great command line utility for automating a lot of common tasks you’ll encounter. It is more like time optimization rather than code optimization. I think it deserves a spot here because it saves a lot of time once setup correctly.

Fastlane是一個很棒的命令行實用工具,用于自動執行許多常見任務。 它更像是時間優化而不是代碼優化。 我認為這里值得一試,因為正確設置后可以節省很多時間。

Checkout fastlane here: https://fastlane.tools/

在此處結帳Fastlane: https : //fastlane.tools/

4:正確處理錯誤 (4: Do error handling correctly)

Don’t expect your users to ping you with exactly how the app crashes. With more complex apps, it is difficult to find specific steps which lead to the app crash. I use sentry.io for error handling on my apps, and I personally like it a lot. It can hook up in your build steps and even upload the sourcemap to their servers so you can see the actual code, not random garbage in your crash traces.

不要期望您的用戶對您的應用崩潰的確切方式進行ping。 對于更復雜的應用程序,很難找到導致應用程序崩潰的特定步驟。 我使用sentry.io來處理我的應用程序中的錯誤,我個人非常喜歡它。 它可以連接您的構建步驟,甚至可以將源映射上傳到它們的服務器,以便您可以查看實際代碼,而不是崩潰跟蹤中的隨機垃圾。

Sentry is available at https://sentry.io/

Sentry可通過https://sentry.io/獲得

5:以正確的方式調試! (5: Do debugging the right way!)

Are you still using that fancy chrome inspect console to debug your React Native apps? And how about Redux? Another tab? What if you want to clear the async storage of your app? Force stop the app and clear data? Seems too tedious especially when you’re actively developing the application. Instead, use a standalone dedicated debugger for react native. Best part? It’s free!

您是否仍在使用該高級chrome檢查控制臺來調試React Native應用程序? 那Redux呢? 另一個標簽? 如果您想清除應用程序的異步存儲該怎么辦? 強制停止應用程序并清除數據? 似乎太繁瑣,尤其是在您積極開發應用程序時。 相反,請使用獨立的專用調試器來響應本機。 最好的部分? 免費!

Here’s your React Native debugger: https://github.com/jhen0409/react-native-debugger

這是您的React Native調試器: https : //github.com/jhen0409/react-native-debugger

5個快速提示: (5 quick tips:)

  • Keep your file structure organized. It is very important to scale your application.

    保持文件結構井井有條。 擴展應用程序非常重要。
  • Avoid using expo for your apps. PLEASE NO. Even if you use it you’ll realize you HAVE to eject at some point, and then good luck figuring out all the mess. It’s not impossible, it will eat a lot of your time later on. Remember, expo is good but we’re talking about long-term business/startup related apps and not a cat age app (for which expo would be good).

    避免將expo用于您的應用程序。 請不。 即使使用它,您也會意識到自己必須在某個時候彈出,然后祝您好運。 這不是不可能,它將在以后花費很多時間。 請記住,expo很好,但是我們在談論的是長期業務/啟動相關的應用程序,而不是貓膩的應用程序(對于expo來說這是個不錯的選擇)。
  • MAKE SURE to create a package-lock.json file (if using npm). You’ll regret it a big time later when you accidentally remove your node_modules folder and realize no package on npm cares about semantic versioning.

    確保創建一個package-lock.json文件(如果使用npm)。 當您不小心刪除了node_modules文件夾并且意識到npm上的任何軟件包都不關心語義版本時,您會后悔了很長時間。
  • Do not use very heavy UI libraries with React Native. It slows down performance even in production. I do not recommend NativeBase as of now, even though it seems very fancy in terms of UI. It’s expensive on performance. There are much better options available like react native paper.

    不要在React Native中使用非常繁瑣的UI庫。 即使在生產中,它也會降低性能。 我現在不建議您使用NativeBase ,即使就UI而言,它似乎也很不錯。 性能上很昂貴。 有很多更好的選擇,例如React Native Paper 。

    Thanks to

    謝謝

    Andre Biel for the comment, please make sure to review this doc page thoroughly if you’re tired of slow RN apps and/or profiling them. It is a goldmine: https://facebook.github.io/react-native/docs/performance.html

    安德烈·比爾 ( Andre Biel)發表評論,如果您對緩慢的RN應用程序和/或對其進行性能分析感到厭倦,請確保徹底閱讀此文檔頁面。 這是一個金礦: https : //facebook.github.io/react-native/docs/performance.html

  • Take advantage of React Native’s replacing JS bundle on the fly without re-submitting the app to app stores using technologies like CodePush.

    利用React Native即時替換JS軟件包的優勢,而無需使用CodePush之類的技術將應用重新提交給應用商店。

  • Get comfortable with at least the basics of native code on both platforms. Especially the build files on Android and pod files on iOS. Those are some files you’ll spend most of your spending-time-on-native time on.

    至少熟悉兩個平臺上的本機代碼基礎。 尤其是Android上的構建文件和iOS上的pod文件。 這些是您將大部分時間花在本機上的文件。

I will continue to write blog posts on React Native as a series of posts, maybe, let’s see!

我將繼續在React Native上寫博客文章,作為一系列文章,也許吧!

有什么問題嗎 (Questions?)

Ask away in the comments below! I’ll be happy to help.

在下面的評論中提問! 我很樂意提供幫助。

Quick shameless plug: If you’re getting started with React Native, here’s my 95% off course on how to get started with it: React Native — The First Steps

快速無恥的插件: 如果您要開始使用React Native,這是我入門的95%課程: React Native —第一步

翻譯自: https://www.freecodecamp.org/news/here-are-my-favorite-hacks-for-creating-production-level-apps-with-react-native-6f0369d879b2/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393232.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393232.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393232.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

HTTP 協議 -- 瀏覽器緩存機制

瀏覽器緩存機制瀏覽器緩存機制主要是 HTTP 協議定義的緩存機制。HTTP 協議中有關緩存的緩存信息頭的關鍵字有 Cache-Control,Pragma,Expires,Last-Modified/ETag 等。瀏覽器請求流程瀏覽器第一請求流程:瀏覽器再次請求流程&#x…

php 獲取實例的類名,PHP類名獲取方式及單例模式實現

類名是什么意思?顧名思義就是各類起了一個名字,java中有兩種數據類型,基本數據類型和引用數據類型,這里類就是引用數據類型,我們在定義一個類的時候必須給類起一個名字,一邊后面的使用比如:int …

CAP理論的理解

CAP理論作為分布式系統的基礎理論,它描述的是一個分布式系統在以下三個特性中: 一致性(Consistency)可用性(Availability)分區容錯性(Partition tolerance)最多滿足其中的兩個特性。也就是下圖所…

開啟真我新格調 期待絢麗的未知

我們每天都在朝幸福努力著,而眼光看的太遠,往往會忘記自己究竟要的是什么。人想要幸福,就得活出真我,當人不能放心大膽地活出自己時,內心會有不安和痛苦。為何要隱藏真正的自己?外界的評判真的那么重要?真我新格調&a…

vuex構建vue項目_如何使用Vue.js,Vuex,Vuetify和Firebase構建單頁應用程序

vuex構建vue項目如何使用Vuetify和Vue路由器安裝Vue并構建SPA (How to install Vue and build an SPA using Vuetify and Vue Router) Do you want to learn how to use Vue.js? Want to create a realistic website using Vue.js? In this tutorial, I will teach you how t…

vim 自動補全

1. vim編輯器自帶關鍵字補全 觸發&#xff1a; ctrl n or ctrl p 補全命令&#xff1a; <C-n> 普通關鍵字 【能夠根據buffer以及標簽文件列表等進行關鍵字補全】 <C-x><C-f> 文件名補全【像在命令行的提示信息一樣&#xff0c;提示當前工…

Linux-RHEL5-初學者配置vsftpd注意事項

我安裝的是RHEL5.4&#xff0c;初學&#xff0c;不在意版本。為了學習方便&#xff0c;安裝操作系統時能選的選項都選全了。事實證明這個決策是正確滴&#xff0c;要不還得花時間學習怎么安裝vsftp。 網上關于如何配置vsftpd的資料挺多的。 我花了小半天的時間&#xff0c;除了…

leetcode459. 重復的子字符串

給定一個非空的字符串&#xff0c;判斷它是否可以由它的一個子串重復多次構成。給定的字符串只含有小寫英文字母&#xff0c;并且長度不超過10000。 示例 1: 輸入: “abab” 輸出: True 解釋: 可由子字符串 “ab” 重復兩次構成。 代碼 class Solution {public boolean r…

解析xml的4種方法詳解

1. 介紹 1&#xff09;DOM(JAXP Crimson解析器) DOM是用與平臺和語言無關的方式表示XML文檔的官方W3C標準。DOM是以層次結構組織的節點或信息片斷的集合。這個層次結構允許開發人員在樹中尋找特定信息。分析該結構通常需要加載整個文檔和構造層次結構&#xff0c;然后才…

php mvc 高性能框架,swoolefy-基于swoole擴展實現的高性能的微服務和mvc框架

swoolefy這是一個基于swoole擴展實現的輕量級高性能的API和Web的MVC微服務框架&#xff0c;參考了TP,Yii2,Workerman&#xff0c;swoole_framework等框架的的設計思想。同時也結合了多年來的技術的積累的以及生產環境中的實踐經驗實現的功能特性1、輕量級的框架,實現路由與調度…

arcore_如何使用ARCore和Android Studio構建增強現實Android應用

arcoreby Ayusch Jain通過Ayusch Jain 如何使用ARCore和Android Studio構建增強現實Android應用 (How to build an Augmented Reality Android App with ARCore and Android Studio) This article was originally posted here本文最初發布在這里 In the previous post, I expl…

最小值的最優化問題

無約束極小值的最優化條件&#xff1a; 關于多元函數極小值點的必要條件&#xff1a; 滿足的點稱之為f(x)的駐點或穩定點&#xff0c;但是反過來&#xff0c;滿足梯度條件的點不一定是f(x)的局部極小值。因此&#xff0c;定理轉化為求解下面的方程組問題&#xff1a; 對于上面…

leetcode950. 按遞增順序顯示卡牌

牌組中的每張卡牌都對應有一個唯一的整數。你可以按你想要的順序對這套卡片進行排序。 最初&#xff0c;這些卡牌在牌組里是正面朝下的&#xff08;即&#xff0c;未顯示狀態&#xff09;。 現在&#xff0c;重復執行以下步驟&#xff0c;直到顯示所有卡牌為止&#xff1a; …

第2課 - 搭建Lua開發環境

第2課 - 搭建Lua開發環境 1. Lua 的優點 &#xff08;1&#xff09;Lua 使用標準的 ANSI C 進行開發&#xff0c;可以無縫集成到宿主程序&#xff0c;且幾乎支持所有平臺。 &#xff08;2&#xff09;Lua 是開源且免費的軟件&#xff0c;以源碼的方式直接發布。開源軟件的發布方…

關于文件系統權限的管理

關于文件系統權限的管理 眾所周知的是&#xff0c;一個操作系統&#xff0c;是需要為不同的用戶進行服務的&#xff0c;因為不能讓每個人都以管理員進行操作&#xff0c;這樣做的話&#xff0c;毫無安全性可言&#xff0c;所以我們需要驗證和授權。這就體現了權限的重要性&…

深度學習工作開展_深入開展深度工作:新經濟中的兩項核心能力

深度學習工作開展by Bar Franek由Bar Franek 深入開展深度工作&#xff1a;新經濟中的兩項核心能力 (Going Deeper on Deep Work: Two Core Abilities for Thriving in the New Economy) 愛深層工作的更多理由 (A Few More Reasons To Love Deep Work) I had no idea my first…

java獲取文件名方法,利用Java獲取文件名、類名、方法名和行號的方法小結

大家都知道&#xff0c;在C語言中&#xff0c;我們可以通過宏FILE、 __LINE__來獲取文件名和行號&#xff0c;而在Java語言中&#xff0c;則可以通過StackTraceElement類來獲取文件名、類名、方法名、行號&#xff0c;具體代碼如下&#xff1a;public static int getLineNumber…

tp5.1升級指導---控制器調整 _initialize方法更改為initialize

控制器調整 為了規范化&#xff0c;繼承了think\Controller類的話&#xff0c;初始化方法從原來的_initialize方法更改為initialize。轉載于:https://www.cnblogs.com/lsyy/p/11027416.html

leetcode537. 復數乘法

給定兩個表示復數的字符串。 返回表示它們乘積的字符串。注意&#xff0c;根據定義 i2 -1 。 示例 1: 輸入: “11i”, “11i” 輸出: “02i” 解釋: (1 i) * (1 i) 1 i2 2 * i 2i &#xff0c;你需要將它轉換為 02i 的形式。 代碼 class Solution {public String c…

初始多態

package Li;public class Person { String name;public Person() {super(); }public Person(String name) {super();this.name name; }public void eat(){System.out.println(name"正在吃面包"); } }package Li;public class Student extends Person {public Studen…