跳到主要内容
新架构实战课 实操 + 基建 + 原理全维度包揽,抢先掌握 React Native 新架构精髓 立即查看 >Version: 0.67

Enabling Fabric on iOS

caution

This documentation is still experimental and details are subject to changes as we iterate. Feel free to share your feedback on the discussion inside the working group for this page.

Moreover, it contains several manual steps. Please note that this won't be representative of the final developer experience once the New Architecture is stable. We're working on tools, templates and libraries to help you get started fast on the New Architecture, without having to go through the whole setup.

This section will go over how to enable the new renderer in your app. Make sure your application meets all the prerequisites.

1. Enable Fabric in Podfile

Add changes to your Podfile. You can see some examples in RNTester and rn-demo-app.

Podfile
# Add the following line at the top of Podfile.
# Codegen produces files/classes that share names, and it will show the warning.
# deterministic_uuids option surpresses the warning.
install! 'cocoapods', :deterministic_uuids => false
target 'Some App' do
pods()
end
def pods()
# Get config
config = use_native_modules!
# Use env variables to turn it on/off.
fabric_enabled = ENV['USE_FABRIC']
use_react_native!(
...
# Modify here if your app root path isn't the same as this one.
:app_path => "#{Dir.pwd}/..",
# Pass the flag to enable fabric to use_react_native!.
:fabric_enabled => fabric_enabled
)
end

2. Update your root view

The way to render your app with Fabric depends on your setup. Here is an example of how you can enable Fabric in your app with the RN_FABRIC_ENABLED compiler flag to enable/disable. Refer RN-Tester’s AppDelegate as an example.

AppDelegate.mm
#ifdef RN_FABRIC_ENABLED
#import <React/RCTFabricSurfaceHostingProxyRootView.h>
#import <React/RCTSurfacePresenter.h>
#import <React/RCTSurfacePresenterBridgeAdapter.h>
#import <react/config/ReactNativeConfig.h>
#endif

@interface AppDelegate () <RCTCxxBridgeDelegate,
RCTTurboModuleManagerDelegate> {
#ifdef RN_FABRIC_ENABLED
RCTSurfacePresenterBridgeAdapter *_bridgeAdapter;
std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig;
facebook::react::ContextContainer::Shared _contextContainer;
#endif

// Find a line that define rootView and replace/edit with the following lines.

#ifdef RN_FABRIC_ENABLED
_contextContainer = std::make_shared<facebook::react::ContextContainer const>();
_reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>();

_contextContainer->insert("ReactNativeConfig", _reactNativeConfig);

_bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc]
initWithBridge:bridge
contextContainer:_contextContainer];

bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;

UIView *rootView =
[[RCTFabricSurfaceHostingProxyRootView alloc] initWithBridge:bridge
moduleName:<#moduleName#>
initialProperties:@{}];
#else
// Current implementation to define rootview.
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:<#moduleName#>
initialProperties:@{}];
#endif

3. Add Babel Plugins

This will trigger the codegen that will run at the metro building time.

babel.config.js
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
'@babel/plugin-proposal-class-properties',
'./node_modules/react-native/packages/babel-plugin-codegen'
]
};

4. Run pod install

// Run pod install with the flags
USE_FABRIC=1 RCT_NEW_ARCH_ENABLED=1 pod install
Mac M1 的注意事项

Cocoapods 目前在 Mac M1 架构上可能还有一些兼容问题(我们建议使用brew install cocoapods来安装 Cocoapods)。如果你在安装 pods 依赖时出现问题,可以尝试运行下面的命令:

  • sudo arch -x86_64 gem install ffi
  • arch -x86_64 pod install

以上命令会安装ffi包,用于在安装和装载 pods 时调用合适的系统架构。