並肩比較React Native和Ionic

字號+ 編輯: 种花家 修訂: IT男在阿里 來源: Code Mentor 2023-09-08 我要說兩句(1)

本文講述了Ionic和React Native (RN)兩種框架的亮點和最大不同,希望對君能對這倆技術産品方向進行明智選擇,以在做相關項目時取得最大收益。

Hybird和原生app

Before diving in the differences between Ionic and RN, let’s briefly remind ourselves what hybrid and native apps are. Hybrid apps are essentially websites embedded in a mobile app through what we call a webview. They are developed using HTML5, CSS, and Javascript, and execute the same code regardless of the platform in which they run. They can, with tools like PhoneGap and Cordova, use the native features of a device, like GPS or camera.

Native apps are developed in the language required by the platform it targets, Objective-C or Swift for iOS, Java for Android, etc. The code written is not shared across platforms and their behavior varies. They have direct access to all features offered by the platform without any restriction.

原生,或hybird?

Ionic

Ionic is a typical hybrid development framework. It uses web technologies to write and render the application, and requires PhoneGap/Cordova bridges to access native features. Then it will try to reproduce native behaviors to provide the best user experience.

What about Ionic Native you’re asking? Don’t be mistaken by the name. Ionic Native is a new name for what used to be called ngCordova. It’s simply the new Ionic way to use existing Cordova and PhoneGap plugins. Ionic Native will not make your app native.

React Native

Developing in React Native is primarily done with Javascript, which means that most of the code you need to get started can be shared across platforms. However, where hybrid apps render using HTML and CSS, React Native will render using native components. This means that the user experience will generally be closer to other native apps as they will follow the patterns imposed by the operating system. Often, this also comes with better performance and smoother animations.

The hiccup is that this only works when the bridging components have been written for React Native. A decent set of native components is provided by default, but if you are a native app developer, do not expect to find all the components you are used to.

总结 Verdict

While the result of RN is native, it requires bridge components to be written for each platform in order to use them, which brings it back closer to a hybrid framework.

跨平台性 Write once, run everywhere

Ionic

A typical hybrid app will run the same code regardless of the platform, and that is what Ionic is capable of doing. However, in order to feel more “native”, Ionic will adapt a few of its behaviors according to the platform. If you use tabs, they will be displayed just as recommended by the platform—at the bottom of the screen in iOS, and at top in Android.

React Native

The goal of RN is not to provide a way to write once, and run everywhere. Rather, they want developers to use the components which best follow the native behaviors of the platform. For example, Android has a highly customizable toolbar; iOS does not. You can use the toolbar for Android but you’ll have to use something different for iOS. Don’t worry, many components have an equivalent; and if there’s no equivalent, it’s easy enough to separate the platform-specific logic.

结论 Verdict

With Ionic, you definitely do not have to worry about platform-specific behaviors. But with React Native you may have to in order to provide the most seamless experience for your users.

语言特性 The language stack

Ionic

Ionic是基于Angular这个JS框架开发的。用HTML模板来做视图。 It is in line with the Mode-View-Controller pattern as views and logic are clearly separated.

React Native

React Native is based on the Javascript framework React and, thus, uses Javascript code that resembles HTML but essentially isn’t—they call it JSX. With that, the display logic is blended in with the business logic. Ultimately, this is not a problem as most developers will feel at ease with JSX. But if you’re working in a team with designers, collaborating with them will not be as straightforward as editing template files and writing CSS. I’m not saying it will take weeks for them to get used to it, but they might need to be taught what is going on. JSX is not HTML; it is styled slightly differently and does not offer the entire set of CSS rules browsers typically support.

结论 Verdict

With Ionic you will be in a more familiar territory: classic HTML and CSS. With React Native, you will have to learn how to style and create your user interfaces using their own HTML-like components, it’s not hard, but it’s new.

Snippet of React’s JSX on the left, Ionic 2 template on the right

工程测试 Testing during development

Ionic

When developing an app, it is very important to get immediate feedback. There is nothing more frustrating than having to wait a while to see if two pixels of margin is looking good or if three pixels is better. With Ionic, you can instantly preview your app in your browser and mobile devices. It instantly refreshes as you make changes to your application.

React Native

Forget about testing in your browser, RN produces native rendering remember? That’s not a problem though, with React Native you see the result of your modifications as you make them. No need to recompile, rebuild, etc.; the result is instant in an emulator or a real device. Pretty amazing!

结论 Verdict

Both are brilliant—but a little different—in that regard. Instant feedback you want; instant feedback you’ll get!

还有什么彩蛋没有?

Ionic

Working with Ionic is a bit like working with a CSS framework like Bootstrap. It comes with lots of components that are pre-made and pre-styled. You want to display a list of items, and for each you need an avatar, a little description, and a date? Done, they have a component for that!

React Native

As React Native wants to rely on native behaviors, they typically do not try to replicate them. The styling is for you to do. Most of the time, only the components which already exist natively are available, rarely non-native ones. However, if you want to write a true native component and use it in your views, you can do so with React Native.

结论 Verdict

Ionic clearly gives you a head start here. Do not worry too much about the styling, apply a few classes and you’re good to go.

插件和社区支持 Plugins and community

When I choose an Open Source project to use, I find it very important to assess how vivid the community around it is. This directly impacts how easily you can find information online, answers to your questions, or getting bugs fixed.

Ionic

PhoneGap and Cordova have been around for a while so there is a high chance that a plugin already exists to expose the native feature you need in your webview. They also have a marketplace where developers sell, and offer, Ionic plugins.

React Native

A lot of plugins are already available on NPM for React Native. There’s even a plugin to use PhoneGap/Cordova plugins, which means that you instantly inherit the hundreds of mature plugins from the other community.

结论 Verdict

最近React Natvie在Github上得到的评星要比Ionic多,各自为 43,700 vs 28,000. Though, Ionic has more questions on Stackoverflow than React Native. They also inherit the community they are based on, namely Angular and React. We can safely say that they both have a very healthy ecosystem.

平台支持性 Supported platforms

Both Ionic and React Native support Android and iOS. And for bonus points, Windows Universal Platform support officially comes with Ionic 2, and is currently maintained as a separate plugin for React Native.

谁更出色? React Native vs Ionic

I can’t tell you which is better because I firmly believe that it depends on many factors: you, your project, your user requirements, the acquired skills of your team, etc. They both do different things, and both do it well. It’s like comparing an Xbox and a Playstation, MacOS and Windows, a car and a motorcycle. Neither is better than the other.

However, I can tell you which one I prefer. It is React Native. I personally value the native rendering more than the rest. To me, apps need to be extremely fast and responsive, and in React Native, I have found a good compromise between writing pure native applications, or pure hybrid apps.

Now, my best advise to you is to try them out both, pick one, and make the best out of it!

Psst. If you’re interested in a code comparison, I wrote a very simplistic todo app for both Ionic and React Native on Github.


閲完此文,您的感想如何?
  • 有用

    0

  • 沒用

    0

  • 開心

    0

  • 憤怒

    0

  • 可憐

    0

1.如文章侵犯了您的版權,請發郵件通知本站,該文章將在24小時内刪除;
2.本站標注原創的文章,轉發時煩請注明來源;
3.交流群: 2702237 13835667

相關課文
  • JS如何防止父節點的事件運行

  • nodejs編寫一個簡單的http請求客戶耑代碼demo

  • 說一則爲什麽後耑開發人員不選擇node.js的原因

  • 使用Sublime Text3 開發React-Native的配置

我要說說
網上賓友點評
1 樓 IP 59.109.***.127 的嘉賓 说道 : 很久前
没翻译完