升级到新版本
升级到新版本的 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-native
、react
和 expo
包版本。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.
2. 升级依赖
¥ Upgrade dependencies
显示的第一个文件是 package.json
,最好更新其中显示的依赖。例如,如果 react-native
和 react
显示为更改,那么你可以通过运行以下命令将其安装到项目中:
¥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:
- npm
- Yarn
# {{VERSION}} and {{REACT_VERSION}} are the release versions showing in the diff
npm install react-native@{{VERSION}}
npm install react@{{REACT_VERSION}}
# {{VERSION}} and {{REACT_VERSION}} are the release versions showing in the diff
yarn add react-native@{{VERSION}}
yarn add 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. In case there are changes you need to manually apply them into your project.
故障排除
¥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.