Skip to main content

升级到新版本

升级到新版本的 React Native 将使你能够访问更多 API、视图、开发者工具和其他好东西。升级需要少量的努力,但我们尽力让你变得简单。

¥Upgrading to new versions of React Native will give you access to more APIs, views, developer tools and other goodies. Upgrading requires a small amount of effort, but we try to make it straightforward for you.

Expo 项目

¥Expo projects

将你的 Expo 项目升级到新版本的 React Native 需要更新 package.json 文件中的 react-nativereactexpo 包版本。Expo 建议逐步升级 SDK 版本,一次一个。这样做将帮助你查明升级过程中出现的故障和问题。有关升级项目的最新信息,请参阅 升级 Expo SDK 演练

¥Upgrading your Expo project to a new version of React Native requires updating the react-native, react, and expo package versions in your package.json file. Expo recommends upgrading SDK versions incrementally, one at a time. Doing so will help you pinpoint breakages and issues that arise during the upgrade process. See the Upgrading Expo SDK Walkthrough for up-to-date information about upgrading your project.

React Native 项目

¥React Native projects

由于典型的 React Native 项目本质上是由 Android 项目、iOS 项目和 JavaScript 项目组成,因此升级可能相当棘手。升级助手 是一款网络工具,可通过提供任意两个版本之间发生的全套更改来帮助你升级应用。它还显示对特定文件的注释,以帮助理解为什么需要进行更改。

¥Because typical React Native projects are essentially made up of an Android project, an iOS project, and a JavaScript project, upgrading can be rather tricky. The Upgrade Helper is a web tool to help you out when upgrading your apps by providing the full set of changes happening between any two versions. It also shows comments on specific files to help understanding why that change is needed.

1. 选择版本

¥ Select the versions

你首先需要选择要升级的版本以及要升级到的版本,默认情况下会选择最新的主要版本。选择后你可以点击按钮 "告诉我如何升级"。

¥You first need to select from and to which version you wish to upgrade, by default the latest major versions are selected. After selecting you can click the button "Show me how to upgrade".

💡 主要更新将在顶部显示 "有用的内容" 部分,其中包含可在升级时帮助你的链接。

¥💡 Major updates will show an "useful content" section on the top with links to help you out when upgrading.

提示

或者你可以运行 npx react-native upgrade,它将自动检查你当前的版本和可用的最新版本,并向你显示指向升级助手页面的链接,其中包含已选择的版本。

¥Or you can run the npx react-native upgrade, which will automatically check your current version and the latest version available and will show you the link to the Upgrade Helper page with the versions already selected.

2. 升级依赖

¥ Upgrade dependencies

显示的第一个文件是 package.json,最好更新其中显示的依赖。例如,如果 react-nativereact 显示为更改,那么你可以通过运行以下命令将其安装到项目中:

¥The first file that is shown is the package.json, it's good to update the dependencies that are showing in there. For example, if react-native and react appears as changes then you can install it in your project by running following commands:

# {{VERSION}} and {{REACT_VERSION}} are the release versions showing in the diff
npm install react-native@{{VERSION}}
npm install react@{{REACT_VERSION}}

3. 升级你的项目文件

¥ Upgrade your project files

新版本可能包含对运行 npx react-native init 时生成的其他文件的更新,这些文件列在升级辅助程序页面中的 package.json 之后。如果没有其他更改,那么你只需要重建项目即可继续开发。

¥The new release may contain updates to other files that are generated when you run npx react-native init, those files are listed after the package.json in the Upgrade Helper page. If there aren't other changes then you only need to rebuild the project to continue developing.

如果有更改,你可以通过复制并粘贴页面中的更改来手动更新它们,也可以通过运行以下命令使用 React Native CLI 升级命令来执行此操作:

¥In case there are changes then you can either update them manually by copying and pasting from the changes in the page or you can do it with the React Native CLI upgrade command by running:

npx react-native upgrade

这将根据最新模板检查你的文件并执行以下操作:

¥This will check your files against the latest template and perform the following:

  • 如果模板中有新文件,则会创建该文件。

    ¥If there is a new file in the template, it is created.

  • 如果模板中的文件与你的文件相同,则会跳过该文件。

    ¥If a file in the template is identical to your file, it is skipped.

  • 如果项目中的文件与模板不同,系统会提示你;你可以选择保留文件或使用模板版本覆盖它。

    ¥If a file is different in your project than the template, you will be prompted; you have options to keep your file or overwrite it with the template version.

某些升级不会使用 React Native CLI 自动补齐,需要手动完成,例如 0.280.29,或 0.560.57。请务必在升级时检查 发行说明,以便你可以识别特定项目可能需要的任何手动更改。

¥Some upgrades won't be done automatically with the React Native CLI and require manual work, e.g. 0.28 to 0.29, or 0.56 to 0.57. Make sure to check the release notes when upgrading so that you can identify any manual changes your particular project may require.

故障排除

¥Troubleshooting

我已完成所有更改,但我的应用仍在使用旧版本

¥I have done all the changes but my app is still using an old version

此类错误通常与缓存有关,建议安装 react-native-clean-project 以清除项目的所有缓存,然后重新运行它。

¥These sort of errors are usually related to caching, it's recommended to install react-native-clean-project to clear all your project's cache and then you can run it again.