简体   繁体   English

React-native和React

[英]React-native and React

I'm building a webapp and ios/android (same app). 我正在构建一个webapp和ios / android(相同的应用程序)。 At first I thought cordova may be a good choice then after reading I thought React-native may be a better choice. 起初我认为cordova可能是一个不错的选择,然后阅读后我认为React-native可能是更好的选择。

My question is: Will I have to write the same app twice (one in react for web and one in reactnative for mobile) ? 我的问题是:我是否必须两次编写相同的应用程序(一个针对网络做出反应,另一个针对移动设备做出反应)?

I've seen some library that can share react-native component for web but I feel I'm gonna be limited. 我见过一些可以共享反应本机组件的库,但我觉得我会受到限制。

What do you think ? 你怎么看 ?

Edit: One more question With react I would use flux (and probably now redux) what should I do to keep my react-native app clean and readable, I mean how should I manage data there ? 编辑:还有一个问题有了反应,我会使用flux(现在可能是redux)我该怎么做才能保持我的本机应用程序的清洁和可读性,我的意思是我应该如何在那里管理数据呢? thanks for your answers already 谢谢你的答案

现在做同样的事情 - 我没有共享代码库,因为网络/移动应用程序的内容不同 - 我使用网络管理和移动应用程序来查看内容。

React native uses the native views UIView, Text etc for display. React native使用本机视图UIView,Text等进行显示。 You can't have the same code base for them. 您不能拥有相同的代码库。 React native uses flex box( it has its own implementation) so that also wont work for web react projects. React native使用flex box(它有自己的实现),因此也不适用于web反应项目。

The only way is to keep them seperate.Also Web and App would be having different design . 唯一的方法是让它们保持独立。而Web和App也会有不同的设计。

While it's true you can't use the exact same code for react native and react JS, you can architect your code base to reuse as much code as possible. 虽然这是真的你不能使用完全相同的代码来反应原生和反应JS,你可以构建你的代码库以尽可能多地重用代码。 Check out this project: 看看这个项目:

https://github.com/benoitvallon/react-native-nw-react-calculator https://github.com/benoitvallon/react-native-nw-react-calculator

Right now you can't share code base b/w React Native / React codes since one relies on native UI code and the other html DOM. 现在你不能共享代码库b / w React Native / React代码,因为一个依赖于本机UI代码和其他html DOM。 There is an interesting project to combine the two ( https://github.com/necolas/react-native-web ) but it's by no means production ready. 有一个有趣的项目将两者结合起来( https://github.com/necolas/react-native-web ),但它绝不是生产准备好的。

I know this may be a late answer but it will be helpful in future. 我知道这可能是一个迟到的答案,但将来会有所帮助。 Check https://github.com/este/este , it has different stacks for mobile, web and server. 检查https://github.com/este/este ,它有不同的移动,Web和服务器堆栈。 You could use common components in common folder. 您可以在公共文件夹中使用常用组件。 It uses firebase as a backend service, but you can change to any service. 它使用firebase作为后端服务,但您可以更改为任何服务。

You actually can use the same code with the use of WebView component in React-Native. 实际上,您可以在React-Native中使用相同的代码和WebView组件。

Here is a link: https://facebook.github.io/react-native/docs/webview.html 这是一个链接: https//facebook.github.io/react-native/docs/webview.html

React Native relies on Native UI code and not html, meaning that all view components will have to be rewritten. React Native依赖于Native UI代码而不是html,这意味着必须重写所有视图组件。 However, if you keep all you logic out of your 'dumb' functional components in your web app, you should be able to reuse a substantial portion of your web code. 但是,如果您将所有逻辑都放在Web应用程序中的“哑”功能组件之外,那么您应该能够重用大部分Web代码。

For instance, you might have an outer container that handles anything you want to happen on mount with componentDidMount, handles and defines onClicks, and determines which view components to render. 例如,您可能有一个外部容器,它使用componentDidMount处理您想要发生的任何事情,处理和定义onClicks,并确定要呈现的视图组件。 This code can be exactly the same in web and mobile, and then your 'dumb' function view components would differ in that one uses native view elements and the other uses HTML. 此代码在Web和移动设备中可以完全相同,然后您的“哑”功能视图组件会有所不同,即使用本机视图元素而另一个使用HTML。

The code for the web-app and the native apps can't be the same (or you might be fine with a WebView wrapper as apps). Web应用程序和本机应用程序的代码不能相同(或者您可能使用WebView包装器作为应用程序)。 I suppose you're asking that because the functionality would be the same for both interfaces. 我想你问的是因为两个接口的功能都是一样的。 The look and feel can be (almost) the same. 外观和感觉可以(几乎)相同。

If you see your apps only as views, the backend (controllers & models) could be shared with multiple platforms. 如果您将应用程序仅视为视图,则可以与多个平台共享后端(控制器和模型)。 You could also try a Backend as a Service like Firebrand or Parse . 您也可以像FirebrandParse一样尝试后端即服务。 Not really an answer on the redux part but it is an easy way to get kickstarted. 对于redux部分,这不是一个真正的答案,但它是一个很容易获得kickstart的方法。

I definitely think you can reuse some of your code. 我绝对认为你可以重用一些代码。 If you're using redux which you probably will if you have some complexity - stuff like actions and reducers are easily reusable. 如果你正在使用redux,如果你有一些复杂性,你可能会使用redux - 诸如动作和减速器之类的东西很容易重复使用。 While the UI components itself is not reusable always you can reuse some of the stuff supporting it. 虽然UI组件本身不可重用,但您总是可以重用一些支持它的东西。 Like timvp mentioned you can also use WebViews to reuse the code but I would not recommend since it does not have the same kind of performance as native modules always. 就像timvp提到的那样,你也可以使用WebViews来重用代码,但我不建议这样做,因为它总是与本机模块没有相同的性能。

I suggest you to look at Redux architecture. 我建议你看看Redux架构。

You will likely get the following folder architecture : 您可能会获得以下文件夹架构:

.
└── src
    ├── actions
    ├── components
    ├── containers
    └── reducers

If you organize well your code, the react-native specific component will only be located inside the component folder. 如果组织好代码,react-native特定组件将只位于组件文件夹中。

All the logic and even the organization is reusable. 所有逻辑甚至组织都是可重用的。

You will rewrite the components with plain html instead of native component. 您将使用纯HTML而不是本机组件重写组件。 And also the apps entrypoints as they are different, but I don't find it is a really big deal. 还有应用程序入口点,因为它们是不同的,但我不认为这是一个非常重要的交易。

More redux examples https://github.com/reactjs/redux/tree/master/examples 更多redux示例https://github.com/reactjs/redux/tree/master/examples

To answer to your second question : Redux is the state(data) manager. 回答第二个问题:Redux是州(数据)经理。 It flows in a unidirectionnal manner. 它以单向方式流动。

For example, how do you handle a user click ? 例如,您如何处理用户点击?

  1. The event is fired from the component 该事件是从组件触发的
  2. The container (which holds the logic) dispatches the action (say BUTTON_CLICK) 容器(保存逻辑)调度动作(比如BUTTON_CLICK)
  3. The reducers receive the BUTTON_CLICK with the current state, only the concerned ones will update the application state (ex: increment a counter) Reducer以当前状态接收BUTTON_CLICK,只有相关的将更新应用程序状态(例如:递增计数器)
  4. When the state is updated, the render tree runs 更新状态后,渲染树将运行

Here is the full explaination of redux flow https://code-cartoons.com/a-cartoon-intro-to-redux-3afb775501a6#.z0bcil3i0 以下是redux流程的完整说明https://code-cartoons.com/a-cartoon-intro-to-redux-3afb775501a6#.z0bcil3i0

To better understand why this mess is awesome read this http://redux.js.org/ 为了更好地理解为什么这个烂摊子很棒,请阅读http://redux.js.org/

You can share most of your code between React and React Native (especially if you use react-native-web . 您可以在React和React Native之间共享大部分代码(特别是如果您使用react-native-web)

Try this article : http://jkaufman.io/react-web-native-codesharing/ It gives a pretty neat example of how to do it. 试试这篇文章: http//jkaufman.io/react-web-native-codesharing/它提供了一个非常巧妙的例子来说明如何做到这一点。

I highly recommend that you keep most of your domain logic (API calls, flux, and even Smart component handling the logic) and only rewrite the dumb components handling the view so that you can implement the best UX for either Desktop or Mobile. 我强烈建议您保留大部分域逻辑(API调用,通量,甚至智能组件处理逻辑),并仅重写处理视图的哑组件,以便您可以为桌面或移动实现最佳UX。

You can name your dumb component view files like so: 你可以像这样命名你的哑组件视图文件:

MyComponent.js (for your web app) MyComponent.native.js (for your native app) or MyComponent.ios.js & MyComponent.android.js if you rather have different views according to the platform. MyComponent.js(对于您的Web应用程序)MyComponent.native.js(对于您的本机应用程序)或MyComponent.ios.js和MyComponent.android.js,如果您根据平台有不同的视图。

The short answer is... 简短的回答是......

YES!... 是!...

You need to write 2 code-base, but read on for the longer answer. 您需要编写2个代码库,但请继续阅读以获得更长的答案。

React Native is created for coding Application using some features of React, while React is created to do Website and Web applications, so they have similarity in nature, syntaxes and library which both using, So basically you need to rewrite some parts of you application, I can say around 20% to 70% percent and it really depends. React Native是为使用React的一些功能编写Application而创建的,而React是为了做网站和Web应用程序而创建的,所以它们在性质,语法和库方面都有相似性,两者都使用,所以基本上你需要重写应用程序的某些部分,我可以说大约20%70% ,这实际上取决于。

I compare React Native and React with some code samples from both for a small component, you can check the differences, but just let you know, for a real application, the difference could much more than what you see here: 我将React NativeReact与两个代码示例进行比较,对于一个小组件,你可以检查差异,但只是让你知道,对于一个真正的应用程序,差异可能比你在这里看到的更多:


React Native 反应原生

React Native lets you build mobile apps using only JavaScript. React Native允许您仅使用JavaScript构建移动应用程序。 It uses the same design as React, letting you compose a rich mobile UI from declarative components. 它使用与React相同的设计,允许您从声明性组件组成丰富的移动UI。

 import React, { Component } from 'react'; import { Text, View } from 'react-native'; class WhyReactNativeIsSoGreat extends Component { render() { return ( <View> <Text> If you like React on the web, you'll like React Native. </Text> <Text> You just use native components like 'View' and 'Text', instead of web components like 'div' and 'span'. </Text> </View> ); } } 

more info here 更多信息在这里


React 应对

React makes it painless to create interactive UIs. React使得创建交互式UI变得轻而易举。 Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes. 为应用程序中的每个状态设计简单视图,当数据发生变化时,React将有效地更新和呈现正确的组件。

Declarative views make your code more predictable and easier to debug. 声明性视图使您的代码更易于预测且更易于调试。

 import React, { Component } from 'react'; class HelloMessage extends React.Component { render() { return ( <div> <p>If you like React Native on the devices, you'll like React.</p> <p>You just use web components like 'div' and 'span', instead of native components like 'View' and 'Text'.</p> </div> ); } } ReactDOM.render(<HelloMessage name="Jane" />, mountNode); 

more info here 更多信息在这里

I wonder why noone is mentioning ReactXP . 我想知道为什么没人提到ReactXP ReactXP is a library maintained by Microsoft that lets you create web, ios, andoid and windows apps with React : ReactXP是一个由Microsoft维护的库,允许您使用React创建web,ios,andoid和windows应用程序:

在此输入图像描述

XP means X-Platform Share most of your code between the web, iOS, Android, and Windows. XP意味着X-Platform在Web,iOS,Android和Windows之间共享大部分代码。

https://microsoft.github.io/reactxp/ https://microsoft.github.io/reactxp/

Other options as mentioned are 提到的其他选项是

React Native is for apps only, it generates fully native components and React Native has specific syntax for its components. React Native仅适用于应用程序,它生成完全本机组件,React Native具有其组件的特定语法。 Using some package to conert React Native code base into web would be noth worth it as you would be limited, and you go against React Native flow, it would be the same as converting Swift or Java written app into web via package. 使用一些软件包将React Native代码库加入到web中将是不值得的,因为您将受到限制,并且您反对React Native流程,它与将Swift或Java编写的应用程序转换为Web程序包相同。 It is not optimal and limited. 它不是最优和有限的。

This is the beauty that you learn javascript and with React(or any other library/framework of your choice) you write best possible web app with web experience, while at the same time using your knowledge on JS you can deliver fully native experience into your apps. 这是您学习javascript和使用React(或您选择的任何其他库/框架)的美丽,您可以使用Web体验编写最佳Web应用程序,同时使用您在JS上的知识,您可以将完全本机体验提供给您应用。

That's about the point of getting close to doing web and native - you can use the same core language and deliver best experiences on both worlds. 这就是接近做网络和原生的关键 - 你可以使用相同的核心语言,并在这两个世界提供最好的体验。

i'm trying to do the same thing. 我正在尝试做同样的事情。 RN is not all the same with React, you will have to deal with native views like: Text , View , ScrollView , ListView , etc. except this, they are nearly the same. RN与React不完全相同,你必须处理原生视图,如: TextViewScrollViewListView等,除此之外,它们几乎相同。

so, you can organize your code like this: root src lib // code for all other three platform android // code for android only ios // code for ios only web // code for web only index.js //for web index.ios.js // for ios index.android.js // for android
所以,你可以像这样组织你的代码: root src lib // code for all other three platform android // code for android only ios // code for ios only web // code for web only index.js //for web index.ios.js // for ios index.android.js // for android
root src lib // code for all other three platform android // code for android only ios // code for ios only web // code for web only index.js //for web index.ios.js // for ios index.android.js // for android

You can share the business logic using the same JavaScript codes (simply by copying the code). 您可以使用相同的JavaScript代码共享业务逻辑(只需复制代码)。 On the other hand, because of the nature of the hardwares, it is completely normal to have different presentation layers for web (mostly on your desktop/laptop browsers) and mobile (native). 另一方面,由于硬件的性质,为Web(主要在您的桌面/笔记本电脑浏览器上)和移动(本机)上提供不同的表示层是完全正常的。

You can refer to react-spa-jwt-authentication-boilerplate (*) as an example. 您可以参考react-spa-jwt-authentication-boilerplate (*)作为示例。 It shares the "business logic" between web and native versions via a folder named common-logic by keeping exact copies of *.js files. 它通过保存*.js文件的精确副本,通过名为common-logic的文件夹common-logic Web和本机版本之间的“业务逻辑”。 On the other hand navigation and presentation layer differs. 另一方面,导航和表示层不同。 It implements a sample authentication process which itself can also be used as a baseline for new projects. 它实现了一个样本身份验证过程,该过程本身也可以用作新项目的基线。

(*) Disclimer: I am the implementer of the repo. (*)Disclimer:我是回购的实施者。 We needed to implement the repo for the exact same need of reducing development time between mobile and web applications. 我们需要实现repo以满足减少移动和Web应用程序之间开发时间的完全相同的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM