You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 3-react-native-internals/3.1-react-native-internals.md
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,48 +1,48 @@
1
1
# React Native Internals
2
2
3
3
React Native is a framework which allows developers to build native apps using Javascript. Wait! Cordova already does that
4
-
and has been around for quite a while. Why will anyone want to use RN?
4
+
and it has been around for quite a while. Why would anyone want to use RN?
5
5
6
-
The primary difference between RN and Cordova based apps is that Cordova based apps run inside a webview while RN apps render using native views. RN apps have direct access to all the Native APIs and views offered by the underlying mobile OS. Thus, RN apps have the same feel and performance of the native application.
6
+
The primary difference between RN and Cordova based apps is that Cordova based apps run inside a webview while RN apps render using native views. RN apps have direct access to all the Native APIs and views offered by the underlying mobile OS. Thus, RN apps have the same feel and performance as that of a native application.
7
7
8
-
At first, its easy to assume that react native might be compiling JS code into the respective native code directly. But this would be really hard to achieve since Java and Objective C are strongly typed languages while Javascript is not! Instead RN does something much much clever. React Native essentially can be considered as a set of React components, where each component represents the corresponding native views and components. For example, a native TextInput will have a corresponding RN component which can be directly imported onto the JS code and used like any other react component. Hence, the developer will be writing the code just like for any other React web app but the output will be a native app.
8
+
At first, it is easy to assume that React Native might be compiling JS code into respective native code directly. But this would be really hard to achieve since Java and Objective C are strongly typed languages while Javascript is not! Instead, RN does something much more clever. Essentially, React Native can be considered as a set of React components, where each component represents the corresponding native views and components. For example, a native TextInput will have a corresponding RN component which can be directly imported into the JS code and used like any other React component. Hence, the developer will be writing the code just like for any other React web app but the output will be a native application.
9
9
10
-
Ok! This all looks black magic 🙄.
10
+
Ok! This looks like black magic 🙄.
11
11
12
-
To understand this, lets take a look at the architecture and how react native works internally.
12
+
To understand this, let us take a look at the architecture and how React Native works internally.
13
13
14
14
### Architecture 🤖
15
15
16
-
Both IOS and Android have a similar architecture with subtle differences.
16
+
Both iOS and Android have a similar architecture with subtle differences.
17
17
18
18
If we consider the big picture, there are three parts to the RN platform:
19
19
20
20
1.**Native Code/Modules** :
21
-
Most of the native code in case of IOS is written in Object C or Swift. While in case of android it is Java.
22
-
But for most cases we will not need to write any native code while writing our react native app.
21
+
Most of the native code in case of iOS is written in Objective C or Swift, while in the case of Android it is written in Java.
22
+
But for writing our React Native app, we would hardly ever need to write native code for iOS or Android.
23
23
24
24
2.**Javascript VM** :
25
-
The JS Virtual Machine that runs all our JavaScript code.
26
-
On iOS/Android simulators and devices React Native uses JavaScriptCore, which is the JavaScript engine that powers Safari.
27
-
JavaScriptCore is an open source JavaScript engine originally built for WebKit.
28
-
Incase of IOS, React Native uses the IOS platform - provided JavaScriptCore.
29
-
It was first introduced in IOS 7 along with OSX Mavericks.<br/>
In case of Android, React Native bundles the JavaScriptCore along with the application. This increases the app size. Hence the hellow world application of RN would take around 3 to 4 megabytes.
33
+
In case of Android, React Native bundles the JavaScriptCore along with the application. This increases the app size. Hence the Hello World application of RN would take around 3 to 4 megabytes for Android.
34
34
35
-
In case of Chrome debugging mode, the JavaScript code runs within Chrome itself (instead of the JavaScriptCore on the device) and communicates with native code via WebSocket. So, here it will use the V8 engine. This allows us to see a lot of information on the chrome debugging tools like network requests, console logs ,etc. 😎
35
+
In case of Chrome debugging mode, the JavaScript code runs within Chrome itself (instead of the JavaScriptCore on the device) and communicates with native code via WebSocket. Here, it will use the V8 engine. This allows us to see a lot of information on the Chrome debugging tools like network requests, console logs, etc. 😎
36
36
37
37
3.**React Native Bridge** :
38
-
React Native bridge is a C++/Java bridge which is responsible for communication between the native and Javascript thread.
39
-
A custom protocol is used for message passing.
38
+
React Native bridge is a C++/Java bridge which is responsible for communication between the native and Javascript thread.
In most cases, a developer would write the entire react native application in Javascript. To run the application one of the following commands are issued via the cli - `react-native run-ios` or `react-native run-android`. At this point React native cli would spawn a node packager/bundler that would bundle the js code into a single `main.bundle.js` file. The packager can be considered something similar to webpack. Now, whenever the react native app is launched, at first the native entry point view is loaded. The Native thread spawns the JS VM thread onto which the bundled JS code is run. The JS code has all the business logic of the application. The Native thread now sends messages via the RN Bridge to start the JS application. Now the spawned Javascript thread starts issuing instructions to the native thread via the RN Bridge. The instructions include what views to load, what information to be retrieved from the hardware, etc. For example, if the js thread wants a view and text to be created it will batch the request onto a single message and send it across to native to render them.
45
+
In most cases, a developer would write the entire React Native application in Javascript. To run the application one of the following commands are issued via the cli - `react-native run-ios` or `react-native run-android`. At this point, React Native cli would spawn a node packager/bundler that would bundle the JS code into a single `main.bundle.js` file. The packager can be considered as being similar to webpack. Now, whenever the React Native app is launched, the first item to be loaded is the native entry point. The Native thread spawns the JS VM thread which runs the bundled JS code. The JS code has all the business logic of the application. The Native thread now sends messages via the RN Bridge to start the JS application. Now, the spawned Javascript thread starts issuing instructions to the native thread via the RN Bridge. The instructions include what views to load, what information is to be retrieved from the hardware, etc. For example, if the JS thread wants a view and text to be created it will batch the request into a single message and send it across to the Native thread to render them.
When a react native application is launched, it spawns up the following threading queues.
59
+
When a React Native application is launched, it spawns up the following threading queues.
60
60
61
61
1.**Main thread**_(Native Queue)_ - This is the main thread which gets spawned as soon as the application launches.
62
62
It loads the app and starts the JS thread to execute the Javascript code. The native thread also listens to the UI events like 'press' , 'touch', etc. These events are then passed onto the JS thread via the RN Bridge. Once the Javascript loads, the JS thread sends the information on what needs to be rendered onto the screen. This information is used by a **shadow node thread** to compute the layouts. The shadow thread is basically like a mathematical engine which finally decides on how to compute the view positions. These instructions are then passed back to the main thread to render the view.
63
63
64
64
2.**Javascript thread**_(JS Queue)_ - The Javascript Queue is the thread queue where main bundled JS thread runs.
65
-
The JS thread runs all the business logic - the code we write in React.
65
+
The JS thread runs all the business logic, i.e., the code we write in React Native.
66
66
67
67
3.**Custom Native Modules** - Apart from the threads spawned by React Native, we can also spawn threads on the custom native modules we build to speed up the performance of the application.
68
68
For example - Animations are handled in React Native by a separate native thread to offload the work from the JS thread.
Here when we write `<Text />`, the Text View manager will invoke `new TextView(getContext())` in case of android.
104
+
Here when we write `<Text />`, the Text View manager will invoke `new TextView(getContext())` in case of Android.
105
105
106
-
View Managers are basically classes extended from `ViewManager` class in Android and subclasses of `RCTViewManager` in IOS.
106
+
View Managers are basically classes extended from `ViewManager` class in Android and subclasses of `RCTViewManager` in iOS.
107
107
108
108
109
109
### Development mode 🔨
110
110
111
-
When the app is run on `DEV` mode ,the Javascript thread is spawned on the development machine. Even though the JS code is running on a more powerful machine as compared to a phone, you will soon notice that the performance is considerably low as compared to when run in bundled mode or production mode. This is unavoidable because a lot more work is done in DEV mode at runtime to provide good warnings and error messages, such as validating propTypes and various other assertions. Furthermore, latency of communication between the device and the JS thread also comes to play here.
111
+
When the app is run on `DEV` mode ,the Javascript thread is spawned on the development machine. Even though the JS code is running on a more powerful machine as compared to a phone, you will soon notice that the performance is considerably lower as compared to when run in bundled mode or production mode. This is unavoidable because a lot more work is done in DEV mode at runtime to provide good warnings and error messages, such as validating propTypes and various other assertions. Furthermore, latency of communication between the device and the JS thread also comes into play.
112
112
113
113
114
114
Link : <https://www.youtube.com/watch?v=8N4f4h6SThc> - RN android architecture
Copy file name to clipboardExpand all lines: README.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,29 +8,29 @@
8
8
<p >Written by <ahref='http://rahulgaba.com'>Rahul Gaba</a> and <ahref='http://atulr.com'>Atul R</a></p>
9
9
10
10
11
-
>A reference for building production grade applications which are easy to test, maintain and extend to multiple platforms. This book is for the Web developers who have already got their hands dirty with react and ES6 and want to build complex native apps.
11
+
>A reference for building production grade applications which are easy to test, maintain and extend to multiple platforms. This book is for the Web developers who have already got their hands dirty with React and ES6 and want to build complex native apps.
12
12
13
13
### You will learn
14
14
15
-
* How react native internally works and how to debug RN apps.
15
+
* How React Native works internally and how to debug RN apps.
16
16
* How to test and write modular code in react-native.
17
-
* Redux: The state container.
18
-
* How to setup a good devops pipeline which will increase team's productivity and ensure seamless testing.
19
-
* How you can extend your react-native codebase to support web or any other platform by just following some code conventions.
17
+
* Redux: the state container.
18
+
* How to set up a good devops pipeline which will increase your team's productivity and ensure seamless testing.
19
+
* How to extend your react-native codebase to support web or any other platform by just following some code conventions.
20
20
21
-
We are following native-first approach with an eye on web. You will eventually see how easy it is port the application to Web by following conventions and stuff.
21
+
We are following a native-first approach while keeping an eye out for potentially extending to web. You will eventually see how easy it is port the application to web by following conventions.
22
22
23
-
The knowledge is based on the experience of working with React Native apps for around 2 years and helping clients launch their apps quicker than ever before.
23
+
Our knowledge is based on our experience of working with React Native apps for around 2 years and helping clients launch their apps quicker than ever before.
24
24
25
25
### This book is for
26
26
27
27
- React developers who are planning to start with react-native applications.
28
-
- Web Developers who know basic ReactJS fundamentals and planning to learn best practices for state management and learn native development.
28
+
- Web Developers who know basic ReactJS fundamentals and want to learn best practices for state management and native development.
29
29
- Native iOS/Android developers who know ReactJS and want to start building apps using React-Native.
30
-
-react-native developers who want to extend their codebase to support other platforms by just following some code conventions.
30
+
-React-Native developers who want to extend their codebase to support other platforms by just following some code conventions.
31
31
32
32
<br/>
33
-
**We will build a [Note Taker](https://github.com/react-made-native-easy/note-taker) application while learning the concepts. There is a link to the code at the end of every chapter. You can also see the app live if you have [Expo](https://expo.io/) app on your phone.**
33
+
**We will build a [Note Taker](https://github.com/react-made-native-easy/note-taker) application while learning the concepts. There is a link to the code at the end of every chapter. You can also see the app live if you have the [Expo](https://expo.io/) app on your phone.**
34
34
35
35
**So be ready to get your hands dirty.**
36
36
@@ -58,15 +58,15 @@ Please star the repo if you like it ;)
58
58
59
59
### DOWNLOAD YOUR COPY
60
60
61
-
Download a .pdf, .epub, or .mobi here. If you prefer offline hard copy. Please feel free to take a printout.
61
+
Download a .pdf, .epub, or .mobi here. If you prefer a hard copy, please feel free to take a printout.
This is an open source book hosted on Github.We will keep updating the contents of the book as and when it gets outdated. Please feel free to contribute or leave a comment in the disqus.
69
+
This is an open source book hosted on Github.We will keep updating the contents of the book as and when it gets outdated. Please feel free to contribute or leave a comment in the disqus.
0 commit comments