简体   繁体   English

升级 react-native 项目的最佳方式

[英]Best way to upgrade react-native project

I have a question about upgrading react-native version.我有一个关于升级 react-native 版本的问题。 We have some choices to upgrade but I don't know differences.我们有一些升级选择,但我不知道有什么区别。

1) react-native upgrade 1) react-native upgrade

2) react-native-git-upgrade 2) react-native-git-upgrade

3) npm install react-native@latest --save then react-native run-android or react-native run-ios 3) npm install react-native@latest --save然后react-native run-androidreact-native run-ios

I used third way for my project because I need to maintain my files in the android folder like MainActivity.java, MainApplication.java, AndroidManifest etc.我对我的项目使用了第三种方式,因为我需要在 android 文件夹中维护我的文件,如 MainActivity.java、MainApplication.java、AndroidManifest 等。

could you please describe differences between these ways?你能描述一下这些方式之间的区别吗?

TL;DR TL; 博士

First, you should check out the latest options for upgrading from facebook.首先,您应该查看从 facebook升级最新选项

If none of those work for you:如果这些都不适合你:

  • Increase version of react-native in your package.json在 package.json 中增加 react-native 的版本
  • run npm install (or yarn if you're using that)运行npm install (或 yarn 如果你正在使用它)
  • run react-native upgrade or possibly react-native upgrade --legacy运行react-native upgrade或可能的react-native upgrade --legacy

Explanation说明

Here I address each of the upgrade options you asked about.在这里,我将介绍您询问的每个升级选项。

1) npm install react-native@latest --save then react-native run-android or react-native run-ios 1) npm install react-native@latest --save然后react-native run-android or react-native run-ios

As you probably know, the run-* commands here don't perform any type of upgrade.您可能知道,此处的run-*命令不执行任何类型的升级。

Meanwhile, npm install --save <library>@<version> is just the command to put a library into your package.json, or update the version of an existing library.同时, npm install --save <library>@<version>只是将库放入 package.json 或更新现有库版本的命令。 This is how you would upgrade the version of any typical library in your package.json.这是升级 package.json 中任何典型库版本的方式。 If that's all there was to upgrading RN, there would be no fuss amongst the community about the difficulty of upgrading.如果这就是升级RN的全部内容,那么社区中就不会对升级的难度大惊小怪了。 There's much more work to do.还有很多工作要做。

If this is the only step you take in upgrading, the new version of react-native will be downloaded to node_modules, but it should fail and complain about many things:如果这是您升级的唯一步骤,新版本的 react-native 将下载到 node_modules,但它应该会失败并抱怨很多事情:

  • react dependency needs to be upgraded as well react依赖也需要升级
  • Files under your ios and android directories will not be in the state which the latest react-native expects.你的iosandroid目录下的文件不会处于最新的react-native期望的状态。 For example, if you upgrade from react-native 0.52 to 0.59, you will have gradle 2 while your react-native library expects gradle 4.例如,如果您从 react-native 0.52 升级到 0.59,您将拥有 gradle 2,而您的 react-native 库需要 gradle 4。
  • Your other dependencies might not work with your newer version of RN.您的其他依赖项可能不适用于较新版本的 RN。 For example, here is a warning from npm install after I did my upgrade:例如,这是我升级后npm install发出的警告:

npm WARN react-native-markdown-renderer@3.2.8 requires a peer of react-native@^0.50.4 but none is installed. You must install peer dependencies yourself.

As you can see, I need to upgrade native-markdown-renderer as well, since it requires RN 0.50 but I've upgraded to 0.59.如您所见,我还需要升级 native-markdown-renderer,因为它需要 RN 0.50,但我已升级到 0.59。 Some libraries might work in this mismatched state, but that is your risk to take.某些库可能会在这种不匹配的状态下工作,但这是您要承担的风险。

2) react-native-git-upgrade 2) react-native-git-upgrade

From what I understand, the RN team had too many problems with this product and no longer want us using it.据我了解,RN 团队对这个产品有太多问题,不再希望我们使用它。 I'm guessing this is why it doesn't work at all for most of us.我猜这就是为什么它对我们大多数人根本不起作用。

3) react-native upgrade 3) react-native upgrade

This will update the version of react-native in your package.json, but then also bring you through a set of guided CLI prompt as it modifies the files in your ios and android directories.这将更新您的 package.json 中的 react-native 版本,但随后还会引导您完成一组引导式 CLI 提示,因为它会修改您的iosandroid目录中的文件。 But how will this guide handle conflicts between the new incoming files and your existing files?但是本指南将如何处理新传入文件和现有文件之间的冲突? You likely have some changes in there you want to keep.您可能想要保留一些更改。

Newer versions of react-native upgrade are said to allow you to perform a diff and merge, but I haven't seen that.据说较新版本的react-native upgrade允许您执行差异和合并,但我还没有看到。 When I ran it, it intended to clobber my entire old file with a new one, and it showed me the path on my local file system to the "new" version that would overwrite my old one.当我运行它时,它打算用一个新文件来破坏我的整个旧文件,并且它向我显示了我本地文件系统上到“新”版本的路径,该“新”版本将覆盖我的旧文件。 So I used my own diffing tool to just diff between the new incoming file and my existing file.所以我使用我自己的比较工具来比较新传入的文件和我现有的文件。 If you lack a diff tool, I use p4Merge .如果您缺少 diff 工具,我会使用p4Merge So, as you go through the CLI guide, just do a diff between your existing file and the path to the new file it gives you, and do that one by one, adding necessary new lines to your files.因此,当您浏览 CLI 指南时,只需在现有文件和它给您的新文件的路径之间做一个差异,然后一一进行,为您的文件添加必要的新行。 If you made some changes, answer "no" to the prompt so that you can keep your old file (with the modifications you just made).如果您进行了一些更改,请对提示回答“否”,以便您可以保留旧文件(以及您刚刚进行的修改)。 If you don't have anything worth keeping in the file, answer "yes" and let the guide simply overwrite and clobber your old file.如果您没有任何值得保留在文件中的内容,请回答“是”,然后让指南简单地覆盖和破坏您的旧文件。

When this command is complete, your ios and android directories will be updated.当这个命令完成后,你的iosandroid目录将被更新。 For example, gradle will be upraded from Gradle 2 to Gradle 4.例如,gradle 将从 Gradle 2 升级到 Gradle 4。

You may hit the bug I did, which causes this command to keep upgrading you to an OLDER version rather than the latest.您可能会遇到我所做的错误,这会导致此命令不断将您升级到旧版本而不是最新版本。 In this case, you need to instead run: react-native upgrade --legacy在这种情况下,您需要运行: react-native upgrade --legacy

If it's a smaller project just create a new react native project using 'npx react-native init yourprojectname --version X.XX.Xt' and then copy the source folder of your older project into the new project.如果它是一个较小的项目,只需使用 'npx react-native init yourprojectname --version X.XX.Xt' 创建一个新的 React Native 项目,然后将旧项目的源文件夹复制到新项目中。 Then try running it in android or ios using 'npx react-native run-android' or 'npx react-native run-ios'.然后尝试使用“npx react-native run-android”或“npx react-native run-ios”在android或ios中运行它。 If there are any runtime errors but no build errors, then update the npm packages accordingly.如果有任何运行时错误但没有构建错误,则相应地更新 npm 包。 Note: This is applicable for small projects because larger projects may contain many 3rd party dependencies.注意:这适用于小型项目,因为大型项目可能包含许多 3rd 方依赖项。 :) :)

I tried react-native-git-upgrade and then deleted node_modules and then npm install it works for me!我尝试了react-native-git-upgrade然后删除了node_modules然后npm install它对我node_modules

https://facebook.github.io/react-native/docs/upgrading https://facebook.github.io/react-native/docs/upgrading

The upgrade approaches mentioned in the other answers do work in many cases, but I have experienced many other cases where there are too many errors.其他答案中提到的升级方法在许多情况下都有效,但我遇到了许多其他错误太多的情况。

From my experience, in these cases the best approach is to create a new project in the new version, and copy the source files to the new project.根据我的经验,在这些情况下,最好的方法是在新版本中创建一个新项目,并将源文件复制到新项目中。

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

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