Skip to content

Commit 75b9b73

Browse files
kakulguptaa7ul
authored andcommitted
Fixes for readme and react native internals chapter (#22)
thanks @kakulgupta
1 parent 3ced66a commit 75b9b73

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

3-react-native-internals/3.1-react-native-internals.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
# React Native Internals
22

33
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?
55

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.
77

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.
99

10-
Ok ! This all looks black magic 🙄.
10+
Ok! This looks like black magic 🙄.
1111

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.
1313

1414
### Architecture 🤖
1515

16-
Both IOS and Android have a similar architecture with subtle differences.
16+
Both iOS and Android have a similar architecture with subtle differences.
1717

1818
If we consider the big picture, there are three parts to the RN platform:
1919

2020
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.
2323

2424
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/>
30-
https://developer.apple.com/reference/javascriptcore.
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 JavaScriptCore provided by the iOS platform.
29+
It was first introduced in iOS 7 along with OS X Mavericks.<br/>
30+
https://developer.apple.com/reference/javascriptcore.
3131

3232

33-
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.
3434

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. 😎
3636

3737
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.
39+
A custom protocol is used for message passing.
4040

4141

4242
![react native architecture diagram](/assets/images/rn-architecture.png)
4343

4444

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 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.
4646

4747
`[ [2,3,[2,'Text',{...}]] [2,3,[3,'View',{...}]] ]`
4848

@@ -56,13 +56,13 @@ MessageQueue.spy(true);
5656

5757
### Threading Model 🚧
5858

59-
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.
6060

6161
1. **Main thread** _(Native Queue)_ - This is the main thread which gets spawned as soon as the application launches.
6262
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.
6363

6464
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.
6666

6767
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.
6868
For example - Animations are handled in React Native by a separate native thread to offload the work from the JS thread.
@@ -101,14 +101,14 @@ AppRegistry.registerComponent('HelloWorldApp', () => HelloWorldApp);
101101
@master-atul/snack_3_1_view_manager
102102
{% endexposnack %}
103103

104-
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.
105105

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.
107107

108108

109109
### Development mode 🔨
110110

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.
112112

113113

114114
Link : <https://www.youtube.com/watch?v=8N4f4h6SThc> - RN android architecture

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@
88
<p >Written by <a href='http://rahulgaba.com'>Rahul Gaba</a> and <a href='http://atulr.com'>Atul R</a></p>
99

1010

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.
1212
1313
### You will learn
1414

15-
* How react native internally works and how to debug RN apps.
15+
* How React Native works internally and how to debug RN apps.
1616
* 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.
2020

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.
2222

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.
2424

2525
### This book is for
2626

2727
- 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.
2929
- 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.
3131

3232
<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.**
3434

3535
**So be ready to get your hands dirty.**
3636

@@ -58,15 +58,15 @@ Please star the repo if you like it ;)
5858

5959
### DOWNLOAD YOUR COPY
6060

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.
6262

6363
https://www.gitbook.com/book/react-made-native-easy/react-made-native-easy/details
6464

6565

6666

6767
### CONTRIBUTIONS
6868

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.
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.
7070

7171

7272
### HALL OF THANKS

0 commit comments

Comments
 (0)