Skip to content

Commit 035b24d

Browse files
authored
[RN][Native components] Update documentation on how to write Native components for 0.77 (facebook#4389)
1 parent f36ad6c commit 035b24d

File tree

2 files changed

+21
-53
lines changed

2 files changed

+21
-53
lines changed

docs/fabric-native-components-ios.md

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,6 @@ using namespace facebook::react;
183183
return concreteComponentDescriptorProvider<CustomWebViewComponentDescriptor>();
184184
}
185185

186-
Class<RCTComponentViewProtocol> WebViewCls(void)
187-
{
188-
return RCTWebView.class;
189-
}
190-
191186
@end
192187
```
193188
@@ -203,40 +198,6 @@ This code is written in Objective-C++ and contains various details:
203198
- the `urlIsValid:(std::string)propString` method that checks whether the URL received as prop is valid;
204199
- the `eventEmitter` method which is a utility to retrieve a strongly typed `eventEmitter` instance
205200
- the `componentDescriptorProvider` which returns the `ComponentDescriptor` generated by Codegen;
206-
- the `WebViewCls` which is an helper method to register the `RCTWebView` in the application.
207-
208-
#### AppDelegate.mm
209-
210-
Finally, you can register the component in the app.
211-
Update the `AppDelegate.mm` to make your application aware of our custom WebView component:
212-
213-
```objc title="Demo/ios/Demo/AppDelegate.mm"
214-
#import "AppDelegate.h"
215-
216-
#import <React/RCTBundleURLProvider.h>
217-
// highlight-start
218-
#import <React/RCTBridge+Private.h>
219-
#import "RCTWebView.h"
220-
// highlight-end
221-
@implementation AppDelegate
222-
// ...
223-
// highlight-start
224-
- (NSDictionary<NSString *,Class<RCTComponentViewProtocol>> *)thirdPartyFabricComponents
225-
{
226-
NSMutableDictionary * dictionary = [super thirdPartyFabricComponents].mutableCopy;
227-
dictionary[@"CustomWebView"] = [RCTWebView class];
228-
return dictionary;
229-
}
230-
// highlight-end
231-
232-
@end
233-
```
234-
235-
This code overrides the `thirdPartyFabricComponents` method by obtaining a mutable copy of the dictionary of third party components coming from other sources, like third party libraries.
236-
237-
It then adds an entry to the dictionary with the name used in the Codegen specification file. In this way, when React is required to load a component with name `CustomWebView`, React Native will instantiate a `RCTWebView`.
238-
239-
Finally, it returns the new dictionary.
240201
241202
#### Add WebKit framework
242203

docs/fabric-native-components.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,24 +120,31 @@ As with Native Modules, you can have multiple specification files in the `specs/
120120
The specification is used by the React Native's Codegen tools to generate platform specific interfaces and boilerplate for us. To do this, Codegen needs to know where to find our specification and what to do with it. Update your `package.json` to include:
121121

122122
```json package.json
123-
"start": "react-native start",
124-
"test": "jest"
125-
},
126-
// highlight-start
127-
"codegenConfig": {
128-
"name": "AppSpec",
129-
"type": "components",
130-
"jsSrcsDir": "specs",
131-
"android": {
132-
"javaPackageName": "com.webview"
133-
}
134-
},
135-
// highlight-end
136-
"dependencies": {
123+
"start": "react-native start",
124+
"test": "jest"
125+
},
126+
// highlight-start
127+
"codegenConfig": {
128+
"name": "AppSpec",
129+
"type": "components",
130+
"jsSrcsDir": "specs",
131+
"android": {
132+
"javaPackageName": "com.webview"
133+
},
134+
"ios": {
135+
"componentProvider": {
136+
"CustomWebView": "RCTWebView"
137+
}
138+
}
139+
},
140+
// highlight-end
141+
"dependencies": {
137142
```
138143

139144
With everything wired up for Codegen, we need to prepare our native code to hook into our generated code.
140145

146+
Note that for iOS, we are declaratively mapping the name of the JS component that is exported by the spec (`CustomWebView`) with the iOS class that will implement the component natively.
147+
141148
## 2. Building your Native Code
142149

143150
Now it's time to write the native platform code so that when React requires to render a view, the platform can create the right native view and can render it on screen.

0 commit comments

Comments
 (0)