我已经开始学习React出于好奇,想知道React和React Native之间的区别-虽然不能找到一个满意的答案使用谷歌。React和React Native似乎有相同的格式。它们的语法完全不同吗?
当前回答
REACT是一个Javascript库,用于构建像Facebook这样的大型/小型界面web应用程序。
REACT NATIVE是一个Javascript框架,用于开发Android、IOS和Windows Phone上的原生移动应用程序。
两者都是由Facebook开源的。
其他回答
我知道已经有很多答案了,但在读完这些之后,我觉得没有人能解释这两者之间的架构差异以及它们是如何工作的,所以我相信仍然有解释的空间。
反应
React = vanilla js + es6 + HTML + CSS = jsx = Web apps(前端)
所以让我们先谈谈React,因为React- native也是基于React的,并且在那里使用了相同的JS概念。
React是一个JS库,用于制作漂亮的,灵活的,高性能的单页web应用程序,所以现在一个问题会出现在你的脑海中什么是单页web应用程序?
单页面应用程序
A single-page application is an app that works inside a browser and does not require page reloading during use. You are using these types of applications every day. These are, for instance: Gmail, Google Maps, Facebook, or GitHub. SPAs are all about serving an outstanding UX by trying to imitate a “natural” environment in the browser — no page reloads, no extra wait time. It is just one web page that you visit which then loads all other content using JavaScript — which they heavily depend on. SPA requests the markup and data independently and renders pages straight in the browser. We can do this thanks to advanced JavaScript frameworks like AngularJS, Ember.js, Meteor.js, Knockout.js, React.js, and Vue.js. Single-page sites help keep the user in one, comfortable web space where content is presented to the user in a simple, easy, and workable fashion.
它是如何工作的
现在你知道SPA是什么了,你知道它是一个web应用程序,所以它会使用HTML元素运行到浏览器中,也会使用JS来处理所有与这些元素相关的功能。 它使用Virtual DOM来呈现组件中的新更改。
React-Native
现在你对react有了一点了解,我们来谈谈react-native
React-Native = React(香草JS + ES6 + JS和本机代码之间的桥梁)+ Native(iOS, Android) =移动应用程序(Android, iOS,也支持web,但有一些限制)
React- native用于使用React制作漂亮的跨平台移动应用程序(Android, iOS)。
它是如何工作的
在React-Native中有两个线程。
JS线程 本机线程
所有的React代码都在JS线程中执行,最终值传递给本机线程,该线程在屏幕上用最终值绘制布局。
JS线程执行所有的计算和传递数据到本机,如何?
React使用Async Bridge以JSON格式将数据传递给Native线程,称为React-Native
注意:新的体系结构不再依赖于桥,它使用JSI和fabric来进行本机代码和JS代码之间的同步通信(这将在下一节中解释)。
因此,我们使用本机组件在react-native中创建表示视图,并使用该桥梁在这两个不同的世界之间进行通信。
JS线程足够快,可以执行JavaScript,本地线程也足够快,可以执行本地代码,但由于React使用异步桥接在这两个世界之间通信,重载这个桥接会导致性能问题。
更新: React-Native现在正在经历一个重新架构的阶段,Facebook团队正试图删除异步桥,以同步地在JS和本机之间通信,这个重新架构的主要部分是JSI(Javascript接口)和结构。
JSI: JSI消除了本地(Java/ObjC)和Javascript代码之间桥梁的需要。它还消除了将所有信息序列化/反序列化为JSON的需求,以便在两个世界之间进行通信。JSI通过将javascript和本地世界结合在一起,为新的可能性打开了大门。
下面是JSI提供的主要功能。
Javascript Interface which allows us to register methods with the Javascript runtime. These methods are available via the global object in the Javascript world. The methods can be entirely written in C++ or they can be a way to communicate with Objective C code on iOS and Java code on Android. Any native module that is currently using the traditional bridge for communication between Javascript and the native worlds can be converted to a JSI module by writing a simple layer in C++ On iOS writing, this layer is simple because C++ can run directly in Objective C hence all the iOS frameworks and code is available to use now. On android however we have to go the extra mile to do this through JNI. These methods can be fully synchronous which means using async/await is not mandatory.
Fabric:根据文档,Fabric是一个新的UI层,允许我们与本地UI组件同步通信。
Fabric是React Native的新渲染系统,是遗留渲染系统的概念进化。核心原则是在c++中统一更多的呈现逻辑,提高与主机平台的互操作性,并为React Native解锁新功能。开发始于2018年和2021年,Facebook应用程序中的React Native得到了新的渲染器的支持。
让我们来谈谈这两个框架的共同点和不同点。
Feature | React | React-Native |
---|---|---|
Platform | Web | Android, IOS, Web |
Open Source | Yes | Yes |
User Interface | HTML + CSS | Native Components(iOS, Android, Web) |
Architecture | Virtual DOM | Virtual DOM + Bridge + Native implementation |
Animations | CSS Animations | Native Animations |
Styling | CSS | JS Stylesheets |
Developed By |
ReactJS是一个JavaScript库,支持前端web和在服务器上运行,用于构建用户界面和web应用程序。它遵循可重用组件的概念。
React Native是一个移动框架,它利用了主机上可用的JavaScript引擎,允许你在JavaScript中为不同的平台(iOS, Android和Windows mobile)构建移动应用程序,允许你使用React js构建可重用的组件,并与本地组件通信
两者都遵循JavaScript的JSX语法扩展。它编译为React。createElement在底层调用。JSX深入
两者都是由Facebook开源的。
有点晚了,但这里有一个更全面的答案,有例子:
反应
React是一个基于组件的UI库,它使用一个“影子DOM”来有效地更新DOM所更改的内容,而不是为每个更改重新构建整个DOM树。它最初是为web应用程序构建的,但现在也可以用于移动和3D/vr。
React和React Native之间的组件不能互换,因为React Native映射到原生移动UI元素,但业务逻辑和非渲染相关的代码可以重用。
ReactDOM
最初包含在React库中,但当React被用于其他平台而不仅仅是web时,它就被分离了。它作为DOM的入口点,并与React联合使用。
例子:
import React from 'react';
import ReactDOM from 'react-dom';
class App extends Component {
state = {
data: [],
}
componentDidMount() {
const data = API.getData(); // fetch some data
this.setState({ data })
}
clearData = () => {
this.setState({
data: [],
});
}
render() {
return (
<div>
{this.state.data.map((data) => (
<p key={data.id}>{data.label}</p>
))}
<button onClick={this.clearData}>
Clear list
</button>
</div>
);
}
}
ReactDOM.render(App, document.getElementById('app'));
反应本地
React Native是一个跨平台的移动框架,它使用React,并通过“桥”在Javascript和它的本地对等物之间进行通信。因此,在使用React Native时,许多UI结构必须有所不同。例如:当构建一个列表时,如果你试图使用map来构建列表而不是React Native的FlatList,你会遇到主要的性能问题。React Native可以用来构建IOS/Android移动应用程序,也可以用于智能手表和电视。
Expo
当启动一个新的React Native应用程序时,Expo是首选。
Expo是一个通用React应用程序的框架和平台。它是一套围绕React Native和原生平台构建的工具和服务,可以帮助您开发、构建、部署和快速迭代iOS、Android和web应用程序
注意:当使用Expo时,你只能使用他们提供的原生Api。你所包含的所有附加库都需要是纯javascript,否则你将需要弹出expo。
使用React Native的相同示例:
import React, { Component } from 'react';
import { Flatlist, View, Text, StyleSheet } from 'react-native';
export default class App extends Component {
state = {
data: [],
}
componentDidMount() {
const data = API.getData(); // fetch some data
this.setState({ data })
}
clearData = () => {
this.setState({
data: [],
});
}
render() {
return (
<View style={styles.container}>
<FlatList
data={this.state.data}
renderItem={({ item }) => <Text key={item.id}>{item.label}</Text>}
/>
<Button title="Clear list" onPress={this.clearData}></Button>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
差异:
Notice that everything outside of render can remain the same, this is why business logic/lifecycle logic code can be re-used across React and React Native In React Native all components need to be imported from react-native or another UI library Using certain API's that map to native components are usually going to be more performant than trying to handle everything on the javascript side. ex. mapping components to build a list vs using flatlist Subtle differences: things like onClick turn into onPress, React Native uses stylesheets to define styles in a more performant way, and React Native uses flexbox as the default layout structure to keep things responsive. Since there is no traditional "DOM" in React Native, only pure javascript libraries can be used across both React and React Native
React360
值得一提的是,React还可以用于开发3D/VR应用程序。组件结构与React Native非常相似。https://facebook.github.io/react-360/
区别很简单。
我们使用React开发web应用程序,使用React Native开发移动应用程序。
React更专注于改进UI(用户界面),而RN则为所有操作系统共享一个公共逻辑层。这为现在已经处理了很长一段时间的问题提供了解决方案,例如开发效率低、部署时间较慢和开发人员生产力较低。
需要注意的是,React是一个用于web开发的库,而React Native是一个平台。
另一件有趣的事情是,当您想要使用RN时,您拥有启动项目所需的一切。另一方面,用React创建一个新项目意味着你只选择了构建web应用程序所需的众多库中的一个。
你可以在那篇文章中找到更多有用的信息: https://pagepro.co/blog/react-native-faq-all-you-need-to-know
反应
React is used for creating websites, web apps, SPAs etc. React is a Javascript library used for creating UI hierarchy. It is responsible for rendering of UI components, It is considered as V part Of MVC framework. React’s virtual DOM is faster than the conventional full refresh model, since the virtual DOM refreshes only parts of the page, Thus decreasing the page refresh time. React uses components as basic unit of UI which can be reused this saves coding time. Simple and easy to learn. React Native React Native is a framework that is used to create cross-platform Native apps. It means you can create native apps and the same app will run on Android and ios. React native have all the benefits of ReactJS React native allows developers to create native apps in web-style approach.