顾 静

顾 静

1655875219

如何将 React Native 组件集成到原生应用程序中

如果没有正确完成,将 React Native 集成到原生应用程序(Android 或 iOS)中可能会很复杂。在本文中,我们的目标是将 React Native 组件集成到原生 Android 应用程序中。

在创建移动应用程序时集成 React Native 的最大好处之一是能够在 iOS 和 Android 应用程序之间重用代码。在将 React Native 集成到现有应用程序后,这提供了几乎完整的代码可重用性,从而大大减少了开发时间和开发成本。

先决条件

  • 一个 React Native 开发环境(React Native CLI);按照此链接获取有关如何使用 React Native CLI 启动和运行的说明
  • Android开发环境(Android Studio)

开始使用我们的 React Native 项目

首先,让我们初始化我们的 React Native 项目。

npx react-native init nativeandroid
cd nativeandroid

启动Metro Bundler并在我们的 React Native 项目文件夹中运行以下命令:

npx react-native start

现在,启动应用程序。

npx react-native run-android

如果设置已正确完成,Android 应用程序应在 Android Studio 模拟器上启动。

React Native 的欢迎页面

设置项目目录

如果没有使用 Android Studio 创建原生 Android 应用程序,则可以创建一个新应用程序。

使用 Android Studio 创建基本 Fragment Android 移动应用

使用 Android Studio,已经创建了一个基本的 Fragment Android 移动应用程序。

安卓手机应用

 

设置文件集成目录

该目录将保存我们的 React Native 应用程序和 Native Android 应用程序。创建一个安卓文件夹:

mkdir ReactNativeIntegration
cd ReactNativeIntegration
mkdir android

导航到使用 Android Studio 创建的 Android 应用程序的根文件夹,将文件中的 Android 应用程序中的所有文件复制AndroidStudioProjects,并将其粘贴到我们刚刚创建的 Android 文件夹中。

将 Android 应用程序文件移动到新的 Android 文件夹中将 Android 应用程序文件移动到新的 Android 文件夹中

package.jsonReactNativeIntegration文件夹中创建一个。

cd ..
touch package.json
code package.json

将下面的代码块添加到空package.json文件中。

{
  "name": "nativeandroid",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "npx react-native start",
    "android": "npx react-native run-android"
  }
}

最后,文件目录应如下所示:

文件目录在最后

安装依赖项

在这里,我们安装:

  • react-native@0.61.5
  • React – 用于构建用户界面的 JavaScript 库
  • hermesvsm– 用于运行我们的 React Native 应用程序的 JavaScript
  • jsc-android– 可维护的构建脚本允许 React Native 将 JSC 的最新版本合并到框架中
yarn add react-native@0.61.5 react hermesvm jsc-android @react-native-community/cli-platform-android@3.0.3

配置 Maven

Maven 是管理和构建我们项目的首选工具。它很方便,因为它会根据项目的需要自动获取依赖项,同时处理传递依赖项。

首先,在本机应用程序的build.gradle文件中打开 Android Studio。

本机应用程序的 build.gradle 文件

将这些代码行添加到依赖项部分:

dependencies {
   //noinspection GradleCompatible
   implementation "com.android.support:appcompat-v7:27.1.1"
   implementation "com.facebook.react:react-native:+" // From node_modules
   implementation "org.webkit:android-jsc:+"
}

我们将利用自动链接的强大功能使我们的应用程序能够访问本地模块并使用 React Native 库提供的本地模块。将代码行添加到build.gradle文件底部的依赖项部分下方。

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

导航到我们文件build.gradle中的androidApplication文件。

Gradle.properties 突出显示

在文件的顶层添加以下代码build.gradle

allprojects {
   repositories {
       maven {
           // All of React Native (JS, Android binaries) is installed from npm
           url ("$rootDir/../node_modules/react-native/android")
       }
       maven {
           // Android JSC is installed from npm
           url("$rootDir/../node_modules/jsc-android/dist")
       }
       google()
       jcenter()

   }
}

task clean(type: Delete) {
   delete rootProject.buildDir
}

settings.gradle中,在底部添加代码行以启用自动链接。

apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)

注释掉dependencyResolutionManagement代码块。

pluginManagement {
   repositories {
       gradlePluginPortal()
       google()
       mavenCentral()
   }
}

//dependencyResolutionManagement {
//    repositorMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
//    repositories {
//        google()
//        mavenCentral()
//    }
//}

同步 Gradle 更改。应该没有错误。

允许访问互联网

AndroidManifest.xml通过将以下代码添加到文件中,可以在文件中启用 Internet 权限AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" /><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.androidapplication">

   <uses-permission android:name="android.permission.INTERNET" />

还应启用 DevSettings 访问权限。

然后,将以下代码添加到AndroidManifest.xml文件中:

<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />       </intent-filter>
   </activity>
   <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>

在创建生产版本构建时,建议剥离 dev 设置权限,因为它仅在从开发服务器重新加载 JavaScript 时需要。

在调试版本中启用明文

由于Android 9(API 级别 28)中引入的一些更改,默认情况下禁用明文流量。因此,我们的应用程序无法连接到 Metro Bundler。

AndroidManifest.xml文件中,将useCleartextTraffic选项添加到调试。

<application
   android:usesCleartextTraffic="true">
</application>

创建发布版本时,这不是强制性的。

为了完善集成过程,将修改原生 Android 应用程序代码以集成 React Native。

将 React Native 代码集成到原生 Android 应用程序中

在我们的 React NativeIntegrate文件夹的根目录中,创建一个index.js文件。

touch index.js

在文件中输入以下代码块index.js

import React from "react";
import { AppRegistry, StyleSheet, Text, View } from "react-native";

const Integration = () => {
  return (
    <View style={styles.container}>
      <Text style={styles.hello}>Hello, World</Text>
    </View>
  );
};
var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
  },
  hello: {
    fontSize: 20,
    textAlign: "center",
    margin: 10,
  },
});

AppRegistry.registerComponent("IntegratedApp", () => Integration);

编写本机代码

为了启动 React Native 运行时并告诉它渲染我们的 JS 组件,我们必须创建一个ReactActivity在 Android Studio 中调用的文件。

在 Android Studio 中打开 Android 项目,创建一个名为的类ReactActivity,它扩展 Activity 并创建一个ReactRootView,它启动一个 React 应用程序并将其设置为

在 Android 项目中创建 ReactActivity 类

将下面的代码块添加到ReactActivity.java文件中。

package com.example.androidapplication;

import android.app.Activity;
import android.os.Bundle;

import com.facebook.react.PackageList;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactPackage;
import com.facebook.react.ReactRootView;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.soloader.SoLoader;

import java.util.List;

public class MyReactActivity extends Activity implements DefaultHardwareBackBtnHandler {
    private ReactRootView mReactRootView;
    private ReactInstanceManager mReactInstanceManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SoLoader.init(this, false);

        mReactRootView = new ReactRootView(this);
        List<ReactPackage> packages = new PackageList(getApplication()).getPackages();
        // Packages that cannot be autolinked yet can be added manually here, for example:
        // packages.add(new MyReactNativePackage());
        // Remember to include them in `settings.gradle` and `app/build.gradle` too.

        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(getApplication())
                .setCurrentActivity(this)
                .setBundleAssetName("index.android.bundle")
                .setJSMainModulePath("index")
                .addPackages(packages)
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();
        // The string here (e.g. "IntegratedApp") has to match
        // the string in AppRegistry.registerComponent() in index.js
        mReactRootView.startReactApplication(mReactInstanceManager, "IntegratedApp", null);

        setContentView(mReactRootView);
    }
    @Override
    public void invokeDefaultOnBackPressed() {
        super.onBackPressed();
    }
}

现在,启动应用程序。

npm run start

应用程序显示 Hello, World

结论

集成 React Native 组件可能是一项艰巨的任务,但开发的好处大于挑战。由于 React Native 是一个开源框架,开发人员可以在将他们的应用程序与 React Native 集成后访问大量共享代码库,从而改善开发人员体验并帮助加快应用程序开发。

我希望这篇文章为您提供了足够的指导。

来源:https ://blog.logrocket.com/integrate-react-native-components-native-apps/

#react-native 

What is GEEK

Buddha Community

如何将 React Native 组件集成到原生应用程序中
Autumn  Blick

Autumn Blick

1598839687

How native is React Native? | React Native vs Native App Development

If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?

In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.

A brief introduction to React Native

Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.

React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.

Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.

Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.

The popularity of React Native comes from its advantages. Some of its advantages are as follows:

  • Performance: It delivers optimal performance.
  • Cross-platform development: You can develop both Android and iOS apps with it. The reuse of code expedites development and reduces costs.
  • UI design: React Native enables you to design simple and responsive UI for your mobile app.
  • 3rd party plugins: This framework supports 3rd party plugins.
  • Developer community: A vibrant community of developers support React Native.

Why React Native is fundamentally different from earlier hybrid frameworks

Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.

React Native is very close to native. Consider the following aspects as described on the React Native website:

  • Access to many native platforms features: The primitives of React Native render to native platform UI. This means that your React Native app will use many native platform APIs as native apps would do.
  • Near-native user experience: React Native provides several native components, and these are platform agnostic.
  • The ease of accessing native APIs: React Native uses a declarative UI paradigm. This enables React Native to interact easily with native platform APIs since React Native wraps existing native code.

Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.

#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms?

React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. Hire Dedicated React Native Developer from top React Native development company, HourlyDeveloper.io to design a spectacular React Native application for your business.

Consult with experts:- https://bit.ly/2A8L4vz

#hire dedicated react native developer #react native development company #react native development services #react native development #react native developer #react native

Hire Dedicated React Native Developers - WebClues Infotech

Being one of the emerging frameworks for app development the need to develop react native apps has increased over the years.

Looking for a react native developer?

Worry not! WebClues infotech offers services to Hire React Native Developers for your app development needs. We at WebClues Infotech offer a wide range of Web & Mobile App Development services based o your business or Startup requirement for Android and iOS apps.

WebClues Infotech also has a flexible method of cost calculation for hiring react native developers such as Hourly, Weekly, or Project Basis.

Want to get your app idea into reality with a react native framework?

Get in touch with us.

Hire React Native Developer Now: https://www.webcluesinfotech.com/hire-react-native-app-developer/

For inquiry: https://www.webcluesinfotech.com/contact-us/

Email: sales@webcluesinfotech.com

#hire react native developers #hire dedicated react native developers #hire react native developer #hiring a react native developer #hire freelance react native developers #hire react native developers in 1 hour

Factors affecting the cost of hiring a React Native developer in USA - TopDevelopers.co

Want to develop app using React Native? Here are the tips that will help to reduce the cost of react native app development for you.
Cost is a major factor in helping entrepreneurs take decisions about investing in developing an app and the decision to hire react native app developers in USA can prove to be fruitful in the long run. Using react native for app development ensures a wide range of benefits to your business. Understanding your business and working on the aspects to strengthen business processes through a cost-efficient mobile app will be the key to success.

#best react native development companies from the us #top react native app development companies in usa #cost of hiring a react native developer in usa #top-notch react native developer in usa #best react native developers usa #react native

How much does it cost to develop a React Native mobile app?

React Native allows developers to develop mobile apps that have compatibility with Android, iOS & other operating systems. Due to the features like Native-like functionality and single code reusability and the access of various frameworks in the market, React Native has excelled as the most suitable framework for cross-platform mobile app development.

Why Do Businesses Prefer React Native App Development?

React Native is integrated with JS library that works as the fundamental for developing the app UI. Most businesses choose for developing React Native apps just due to their cross-platform & open-source features. A few further reasons why entrepreneurs & developers choose React Native app development include:

• Lowered Expedition Time

• Simple UI

• Cross-Platform and Code Sharing

• Lesser Workforce and Resources

• Community Assistance

• In-Built Elements and Reusable Codes

• Hot Reload

• JavaScript as Programming Language

• Easy to Execute Updates

Factors That Decide Cost of React Native App Development

If you are an entrepreneur or start-up and looking for cost-effective app development, React Native is one of the ideal options available out there.

• App’s UI/UX Design

• User Authorization

• App Complexity and Functionality

• App Development Team

• App Maintenance

• App Add-ons

• App Distribution

• Location of Development Company

• App Category

React Native cost depends widely on the complexity of a project or the app requirements. The price may also vary based on business requirements. React Native app development per hour can cost from $20 and $30 per hour in India. It can vary as per different locations.

Is React Native a good choice for mobile apps development?

Yes, React Native is the best choice for mobile app development as React apps are faster to develop and it offers better quality than hybrid apps. Additionally, React Native is a mature cross-platform framework.

Best React Native App Development Agency

AppClues Infotech is a leading React Native App Development Company in USA that build robust & innovative mobile app as per your specific business needs. They have a dedicated team of designers and programmers help to make a perfect mobile app.

If you have any mobile app development project in mind get in touch with AppClues Infotech and get the best solution for your business.

#react native app development cost #react native development company #best react native development company in usa #hire react native developers #hire dedicated react native developers & programmers #hire a react native development company