React Native 使用什么来允许 JavaScript 在 iOS 和 Android 上本地执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33479180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does React Native use to allow JavaScript to be executed on iOS and Android natively?
提问by kharandziuk
What does React Native use under the covers to interface with iOS and Android? Cordova uses a WebView to effectively display a webpage inside of a native container; Does React Native use the same approach? If not, What approach does it use?
React Native 在幕后使用什么与 iOS 和 Android 交互?Cordova 使用 WebView 在原生容器内有效地显示网页;React Native 是否使用相同的方法?如果不是,它使用什么方法?
采纳答案by AMilassin
As you noticed React Native is not based on Cordova. It is not a website which looks like an app shoveled into a WebView.
正如您所注意到的,React Native 不是基于 Cordova。它不是一个看起来像一个应用程序的网站。
React Native uses a JavaScript runtime, but the UI is not HTML and it doesn't use a WebView. You use JSX and React Native specific components to define the UI.
React Native 使用 JavaScript 运行时,但 UI 不是 HTML 并且不使用 WebView。您使用 JSX 和 React Native 特定组件来定义 UI。
It provides a native-level performance and look and feel but some UI parts have to be configured separately for iOS and Android. For example Toolbars are completely different, but TextInput can be the same for both Operating Systems.
它提供了本机级别的性能和外观,但必须为 iOS 和 Android 分别配置一些 UI 部分。例如,工具栏是完全不同的,但 TextInput 对于两个操作系统可以是相同的。
回答by Manish Patwari
回答by anonymous
To run Javascript code
运行 Javascript 代码
React Native uses JavaScriptCore (JavaScript engine that powers Safari) on Android/ iOS simulators and devices.
React Native 在 Android/iOS 模拟器和设备上使用 JavaScriptCore(支持 Safari 的 JavaScript 引擎)。
In case of Android, React Native bundles the JavaScriptCore along with the application.
对于 Android,React Native 将 JavaScriptCore 与应用程序捆绑在一起。
In case of iOS, React Native uses the JavaScriptCore provided by the iOS platform.
对于 iOS,React Native 使用 iOS 平台提供的 JavaScriptCore。
To communicate with Android/ iOS
与 Android/iOS 通信
React Native bridge (C++/Java bridge) is used. It is responsible for communication between the native and Javascript thread.
使用 React Native 桥(C++/Java 桥)。它负责本机线程和 Javascript 线程之间的通信。
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
orreact-native run-android
. At this point, React Native CLI would spawn a node packager/bundler that would bundle the JS code into a singlemain.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. Source
在大多数情况下,开发人员会用 Javascript 编写整个 React Native 应用程序。要运行应用程序,请通过 CLI 发出以下命令之一 -
react-native run-ios
或react-native run-android
. 此时,React Native CLI 将产生一个节点打包器/打包器,它将 JS 代码打包成一个main.bundle.js
文件。可以认为打包器类似于 Webpack。现在,每当 React Native 应用程序启动时,要加载的第一个项目就是本机入口点。Native 线程产生运行捆绑的 JS 代码的 JS VM 线程。JS 代码具有应用程序的所有业务逻辑。Native 线程现在通过 RN Bridge 发送消息以启动 JS 应用程序。现在,生成的 Javascript 线程开始通过 RN Bridge 向本机线程发出指令。指令包括加载哪些视图,从硬件中检索哪些信息等。 来源