什么是本机链接? [英] What is react-native link?

查看:47
本文介绍了什么是本机链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

react-native link 命令的目的是什么?

What is the purpose of the react-native link command?

推荐答案

注意: 来自 React-Native 0.60.0 使用 react-native link 链接包已经变得多余.Autolink 已添加到 React-Native CLI,这意味着 iOS 现在将使用 cocoapods,而 Android 将使用 gradle.您可以在此处阅读有关自动链接的更多信息.

Note: from React-Native 0.60.0 linking packages using react-native link has become redundant. Autolink has been added to the React-Native CLI which means that iOS will now use cocoapods and Android will use gradle. You can read more about Autolinking here.

什么是react-native link?

react-native link 是安装本机依赖项的自动方式.它是手动链接项目中依赖项的替代方法.它适用于 Android 和 iOS.

What is react-native link?

react-native link is an automatic way for installing native dependencies. It is an alternative to manually linking the dependency in your project. It works for both Android and iOS.

手动链接项目时,大部分步骤是相同的​​,因此使用 react-native link 可以让您轻松安装本机依赖项,​​而无需键入类似的代码或执行类似的操作动作反复.

When linking a project manually the majority of the steps are the same and so using react-native link allows you to install the native dependency with less fuss and without having to type similar code or perform similar actions repeatedly.

然而,应该注意的是,运行 react-native link 并不总是完全链接一个包,有时需要额外的步骤,你应该仔细检查安装指示.

However, it should be noted that running react-native link will not always link a package fully, sometimes additional steps are required and you should check carefully with the installation instructions.

在安装依赖项并链接它之前,请务必仔细阅读说明.

如果您的项目正在使用 CocoaPods 并且您正在链接的依赖项具有 .podspec 那么当您使用 run react-native link它将更新您的 Podfile.这不是将文件直接添加到您的 Xcode 项目中.您还必须在 ios 目录中运行 pod install 否则本机依赖项将无法完全安装.

If your project is using CocoaPods and the dependency that you are linking has a .podspec then when you use run react-native link it will update your Podfile. This is instead of adding the files directly to your Xcode project. You will also have to run pod install inside your ios directory otherwise the native dependency won't be fully installed.

有时使用 CocoaPods 进行安装会导致更多问题,并且并非每个依赖项都需要使用 CocoaPods 安装,您可以始终按照我在此 SO 中概述的步骤进行操作 answer 停止 react-native linkPodfile 添加依赖项,这并不理想,但它是一种解决方法.某些依赖项需要在 Podfile 中添加内容,因此只有在依赖项不需要 pods 运行时才应该这样做.

Sometimes installing using CocoaPods can cause more issues, and not every dependency needs to be installed with CocoaPods you could always follow the steps that I outlined in this SO answer to stop react-native link adding a dependency to the Podfile, it is not ideal but it is a workaround. Some dependencies require additions to be made to the Podfile, so you should only do this if the dependency doesn't require pods to run.

你应该在链接任何依赖时只使用 react-native link 还是应该更明确地使用 react-native link dependency-name?

Should you just use react-native link when linking any dependency or should you be more explicit and use react-native link dependency-name?

根据我的经验,最好使用 react-native link dependency-name.这是因为 react-native link 将尝试链接(或重新链接)所有可以链接的依赖项,这可能导致代码重复.我遇到的大多数问题都是在链接 Android 本机依赖项时.我认为在随后的更新中阻止这种情况发生已经取得了一些进展,但古老的格言在这里适用一次被咬,两次害羞

From my experience it is better to use react-native link dependency-name. This is due to the fact that react-native link will try to link (or re-link) all the dependencies that can be linked and this can lead to code duplication. Most of the issues that I have experienced have been when the Android native dependency is being linked. I think there has been some headway in stopping this from happening in subsequent updates, but the old adage applies here once bitten, twice shy

在使用 react-native link dependency-name 时,您应该遵循良好的做法,以免被刺痛.有时,您尝试的依赖项不会按预期工作,并且删除在链接过程中添加的所有代码可能很棘手.(如果您不熟悉 Xcode 项目文件可能是一场噩梦).

When using react-native link dependency-name you should follow good practice so that you don't get stung. Sometimes dependencies that you try don't work as expected and removing all the code that was added during the linking process can be tricky. (Xcode project files can be a nightmare to go through if you are unfamiliar with them).

这就是我安装依赖项然后链接它们的方式.

This is how I install dependencies and then link them.

  1. 确保您使用的是版本控制,例如 git.
  2. 确保您的代码完全提交,没有未保存的更改.
  3. 创建一个新分支,并检查它.
  4. 安装你的依赖 npm i dependency-name
  5. 然后链接你的依赖 react-native link dependency-name
  6. 执行依赖项所需的任何其他安装步骤.请参阅依赖项的安装说明.
  7. 检查您的代码是否适用于新的依赖项.
  8. commmit 更改并合并分支.
  1. Make sure that you are using version control, like git.
  2. Make sure your code is fully committed with no unsaved changes.
  3. Create a new branch, and check it out.
  4. Install your dependency npm i dependency-name
  5. Then link you dependency react-native link dependency-name
  6. Perform any additional installation steps that the dependency requires. See the installation instructions for the dependency.
  7. Check that your code works with the new dependency.
  8. commmit changes and merge the branch.

<小时>

手动链接

如果您更喜欢手动链接您的原生依赖项,那么您应该按照依赖项网站上的说明进行操作,或者您可以查看 react-native 提供的文档.


Manual Linking

If you prefer to link your native dependencies manually then you should either follow the instructions on the dependency's website or you can look at the documentation that react-native provides.

目前只有关于如何手动链接的说明 iOS 项目.

Currently there is only an explanation on how to manually link iOS projects.

手动链接 Android 需要您在以下位置进行更改:

Manually linking Android requires you to make changes in the following locations:

  1. settings.gradle
  2. app/build.gradle
  3. MainApplication.java

对于您应该进行的确切更改,您应该查看依赖项的手动链接说明.

As always for the exact changes that you should make you should look at the dependency's manual linking instructions.

这取决于您使用的依赖项,某些依赖项仅使用用 Javascript 编写的代码,因此不需要链接它们,并且运行 react-native link dependency-name.

It depends on the dependency that you are using some dependencies use only code written in Javascript, so it is not required to link them, and there is no benefit served by running react-native link dependency-name.

但是,如果依赖项包含本机代码,则您必须进行链接.手动或使用 react-native link dependency-name.

However, if the dependency contains native code then you will have to link. Either manually or by using react-native link dependency-name.

首先,您需要检查网站、github 存储库或 npmjs.com 页面的依赖项.在安装依赖项后,通常会有说明告诉您是否链接依赖项.

Firstly you need to check the website, the github repo, or the npmjs.com page for the dependency. There will usually be instructions there telling you whether to link the dependency after you have installed it.

如果您找不到有关链接的任何说明,则您(可能)不需要链接它.

If you cannot find any instructions about linking, you (probably) won't need to link it.

如果您仍然不确定,请咨询依赖项维护者.

If you are still unsure, check with the dependency maintainer.

是的,如果没有可链接的内容,您就什么也做不了.但始终使用 react-native link dependency-name 运行它以避免出现问题.

Yes, you can it won't do anything if there is nothing to link. But always run it with react-native link dependency-name to avoid issues.

您只能在安装依赖项后运行它.我建议您在安装依赖项后立即运行它.然后,在安装任何新的依赖项之前,您应该检查以确保它可以正常工作,以便您可以轻松调试.

You only run it after you have installed your dependency. I would recommend running it just after you have installed the dependency. You should then check to make sure that it works, before installing any new dependencies so that you can easily debug.

对于每个依赖项,您不应需要多次运行它.

You shouldn't need to run it more than once per dependency.

无论您对 javascript 代码做了多少个组件或更改,都不会影响链接,因为链接是纯原生的,而组件是 javascript.

It doesn't matter how many components or changes to the javascript code that you make it won't affect the linking, as the linking is purely native and components are javascript.

自动链接是添加到 react-native-cli 的新功能.您可以在此处阅读有关自动链接的更多信息.

Autolink is a new feature that is being added to the react-native-cli. You can read more about autolink here.

Autolink 替换 react-native-link

自动链接是一种内置于 CLI 的机制,允许添加React Native 的原生组件依赖就这么简单:

Autolinking is a mechanism built into CLI that allows adding a dependency with native components for React Native to be as simple as:

yarn 添加 react-native-webview

自动链接是 react-native 链接的替代品,它带来了新功能(例如在 iOS 上轻松集成本机依赖项的能力)并修复了一些长期存在的问题.

一旦它完全实现,它应该可以更轻松地将带有本机代码的依赖项添加到您的项目中.

Once it is fully implemented it should make adding dependencies with native-code to your project much easier.

这篇关于什么是本机链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆