React 18 与 React Native
本文档内容仍处于实验阶段,内容会随着版本的迭代进行修改。您可随时在我们的工作组的讨论区发送反馈。 此外,本文档还包含了若干需手动配置的步骤,但这不代表新架构稳定版的最终开发体验。我们仍在开发相关的工具、模板和第三方库,帮助你更快地迁移到新架构上,而非从头开始配置环境。
This page describes how to use React 18 with React Native using the React Native's New Architecture.
简而言之: 第一个和 React 18 兼容的 React Native 版本是 0.69.0. In order to use the new features in React 18 including automatic batching,
startTransition
, anduseDeferredValue
, you must migrate your React Native app to the New Architecture.
React 18 与 React Native 的新架构
React 18 引入了 多个全新特性:
- 自动批处理状态变更 (Automatic batching)
- 新的严格模式 (Strict Mode)
- 新的 hooks (
useId
,useSyncExternalStore
)
It also includes new concurrent features:
startTransition
useTransition
useDeferredValue
- Full Suspense support
The concurrent features in React 18 are built on top of the new concurrent rendering engine. Concurrent rendering is a new behind-the-scenes mechanism that enables React to prepare multiple versions of your UI at the same time.
Previous versions of React Native built on the old architecture cannot support concurrent rendering or concurrent features. This is because the old architecture relied on mutating the native trees, which doesn’t allow for React to prepare multiple versions of the tree at the same time.
Fortunately, the New Architecture was written bottom-up with concurrent rendering in mind, and is fully compatible with React 18. This means, in order to upgrade to React 18 in your React Native app, your application needs to be migrated to the React Native's New Architecture including Fabric and TurboModules.
React 18 默认启用
Starting in React Native 0.69, React 18 is enabled by default when you enable the New Architecture.
The means you’re able to use the new features in React 18 as soon as you migrate. Since the new concurrent features are opt-in by using features like startTransition
or Suspense
, we expect React 18 to work out-of-the-box with minimal changes for users who migrate to the New Architecture or create a new app with the New Architecture enabled.
However, if you do hit any issues, we provide an option to opt-out of the new root in React 18. Opt-ing out means your app will run in React 17 mode, and none of the features of React 18 will be accessible.
Opt-ing out of React 18 on Android
On Android, you will be able to override the isConcurrentRootEnabled
in your ActivityDelegate (in the MainActivity
file), and enable/disable Concurrent React.
- Java
- Kotlin
public class MainActivity extends ReactActivity {
public static class MainActivityDelegate extends ReactActivityDelegate {
public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
super(activity, mainComponentName);
}
@Override
protected ReactRootView createRootView() {
ReactRootView reactRootView = new ReactRootView(getContext());
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
return reactRootView;
}
+ @Override
+ protected boolean isConcurrentRootEnabled() {
+ // If you opted-in for the New Architecture, we enable Concurrent Root (i.e. React 18).
+ // More on this on https://reactjs.org/blog/2022/03/29/react-v18.html
+ return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
+ }
}
}
class MainActivity : ReactActivity() {
open class MainActivityDelegate(activity: ReactActivity?, mainComponentName: String?) : ReactActivityDelegate(activity, mainComponentName) {
override fun createRootView(): ReactRootView = ReactRootView(context).apply {
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED)
}
+ // If you opted-in for the New Architecture, we enable Concurrent Root (i.e. React 18).
+ // More on this on https://reactjs.org/blog/2022/03/29/react-v18.html
+ override fun isConcurrentRootEnabled() = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
}
}
Opt-ing out of React 18 on iOS
On iOS, you'll have access to the concurrentRootEnabled
method on your AppDelegate.mm
file. You should change the returned value to false
(or NO
) to disable the feature.
/// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
///
/// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
/// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
/// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
- (BOOL)concurrentRootEnabled
{
// Switch this bool to turn on and off the concurrent root
return true;
}
Users on React Native 0.69 not yet migrated to the New Architecture
Note: Users on React Native 0.69, but still on the Old Architecture will still use React 17 mode even if React 18 is installed in the package.json
file.
Overriding the isConcurrentRootEnabled
method will have no effect on your app.