Skip to content

Commit c2a3857

Browse files
committed
0.76
1 parent 342c565 commit c2a3857

File tree

356 files changed

+49582
-838
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

356 files changed

+49582
-838
lines changed

cndocs/_markdown-m1-cocoapods.mdx

Lines changed: 0 additions & 11 deletions
This file was deleted.

cndocs/appendix.md

Lines changed: 18 additions & 278 deletions
Original file line numberDiff line numberDiff line change
@@ -6,290 +6,30 @@
66

77
- **Fabric 原生组件** - 指已经适配以与新架构(即新渲染器)良好协同工作的组件。为简洁起见,您可能会看到它们被称为**Fabric 组件**
88
- **Turbo 原生模块** - 指已经适配以与新架构(即新原生模块系统)良好协同工作的模块。为简洁起见,您可能会看到它们被称为**Turbo 模块**
9-
109
- **传统原生组件** - 指运行在旧版 React Native 架构上的组件。
1110
- **传统原生模块** - 指运行在旧版 React Native 架构上的模块。
12-
1311

14-
## II. Flow 类型到原生类型的映射
12+
## II. Codegen 类型
1513

16-
您可以使用以下表格作为参考,了解每个平台支持哪些类型以及它们在各个平台上的映射关系
14+
您可以使用以下表格作为参考,了解每种类型在不同平台上的支持情况
1715

18-
### `string`
16+
| Flow | TypeScript | Flow Nullable Support | TypeScript Nullable Support | Android (Java) | iOS (ObjC) |
17+
| -------------------------------------------------------------------------- | --------------------------------------------------- | ------------------------------------------------------- | ---------------------------------------------------- | ------------------------------------ | -------------------------------------------------------------- |
18+
| `string` | `string` | `?string` | <code>string &#124; null</code> | `string` | `NSString` |
19+
| `boolean` | `boolean` | `?boolean` | <code>boolean &#124; null</code> | `Boolean` | `NSNumber` |
20+
| Object Literal<br /><code>&#123;&#124; foo: string, ...&#124;&#125;</code> | <code>&#123; foo: string, ...&#125; as const</code> | <code>?&#123;&#124; foo: string, ...&#124;&#125;</code> | <code>?&#123; foo: string, ...&#125; as const</code> | \- | \- |
21+
| Object [[1](#notes)] | Object [[1](#notes)] | `?Object` | <code>Object &#124; null</code> | `ReadableMap` | `@` (untyped dictionary) |
22+
| <code>Array&lt;T&gt;</code> | <code>Array&lt;T&gt;</code> | <code>?Array&lt;T&gt;</code> | <code>Array&lt;T&gt; &#124; null</code> | `ReadableArray` | `NSArray` (or `RCTConvertVecToArray` when used inside objects) |
23+
| `Function` | `Function` | `?Function` | <code>Function &#124; null</code> | \- | \- |
24+
| <code>Promise&lt;T&gt;</code> | <code>Promise&lt;T&gt;</code> | <code>?Promise&lt;T&gt;</code> | <code>Promise&lt;T&gt; &#124; null</code> | `com.facebook.react.bridge.Promise` | `RCTPromiseResolve` and `RCTPromiseRejectBlock` |
25+
| Type Unions<br /><code>'SUCCESS'&#124;'FAIL'</code> | Type Unions<br /><code>'SUCCESS'&#124;'FAIL'</code> | Only as callbacks | | \- | \- |
26+
| Callbacks<br />`() =>` | Callbacks<br />`() =>` | Yes | | `com.facebook.react.bridge.Callback` | `RCTResponseSenderBlock` |
27+
| `number` | `number` | No | | `double` | `NSNumber` |
1928

20-
<VerticalTable data={[
21-
['Nullable Support?', <code>?string</code>],
22-
['Android (Java)', <code>String</code>],
23-
['iOS', <code>NSString</code>],
24-
]} />
29+
### Notes:
2530

26-
### `boolean`
31+
<b>[1]</b> 我们强烈建议使用对象字面量而不是对象。
2732

28-
<VerticalTable data={[
29-
['Nullable Support?', <code>?boolean</code>],
30-
['Android (Java)', <code>Boolean</code>],
31-
['iOS', <code>NSNumber</code>],
32-
]} />
33-
34-
### 对象字面量
35-
36-
比起普通的`Object`来说我们更推荐使用对象字面量,其更具有类型安全性。
37-
38-
**示例:** `{| foo: string, ... |}`
39-
40-
<VerticalTable data={[
41-
['Nullable Support?', <code>{`?{| foo: string, ...|}`}</code>],
42-
['Android (Java)', '-'],
43-
['iOS', '-'],
44-
]} />
45-
46-
### `Object`
47-
48-
:::note
49-
推荐使用[对象字面量](#object-literal)
50-
:::
51-
52-
<VerticalTable data={[
53-
['Nullable Support?', <code>?Object</code>],
54-
['Android (Java)', <code>ReadableMap</code>],
55-
['iOS', <><code>@{}</code> (untyped dictionary)</>],
56-
]} />
57-
58-
### `Array<*>`
59-
60-
<VerticalTable data={[
61-
['Nullable Support?', <code>{`?Array<*>`}</code>],
62-
['Android (Java)', <code>ReadableArray</code>],
63-
['iOS', <><code>NSArray</code> (or <code>RCTConvertVecToArray</code> when used inside objects)</>],
64-
]} />
65-
66-
### `Function`
67-
68-
<VerticalTable data={[
69-
['Nullable Support?', <code>?Function</code>],
70-
['Android (Java)', '-'],
71-
['iOS', '-'],
72-
]} />
73-
74-
### `Promise<*>`
75-
76-
<VerticalTable data={[
77-
['Nullable Support?', <code>{`?Promise<*>`}</code>],
78-
['Android (Java)', <code>com.facebook.react.bridge.Promise</code>],
79-
['iOS', <><code>RCTPromiseResolve</code> and <code>RCTPromiseRejectBlock</code></>],
80-
]} />
81-
82-
### 类型联合
83-
84-
类型联合仅支持作为回调函数。
85-
86-
**Example:** `'SUCCESS' | 'FAIL'`
87-
88-
<VerticalTable data={[
89-
['Nullable Support?', 'Only as callbacks.'],
90-
['Android (Java)', '-'],
91-
['iOS', '-'],
92-
]} />
93-
94-
### 回调函数
95-
96-
回调函数没有类型检查,并且被泛化为`Object`
97-
98-
**Example:** `() =>`
99-
100-
<VerticalTable data={[
101-
['Nullable Support?', 'Yes'],
102-
['Android (Java)', <code>com.facebook.react.bridge.Callback</code>],
103-
['iOS', <code>RCTResponseSenderBlock</code>],
104-
]} />
105-
106-
:::note
107-
您可能还会发现参考 React Native 中核心模块的 JavaScript 规范很有用。这些规范位于 React Native 存储库中的`Libraries/`目录内。
33+
:::info
34+
您也可以参考 React Native 核心模块的 JavaScript 规范。这些位于 React Native 仓库的 [`Libraries/`](https://github.com/facebook/react-native/tree/main/packages/react-native/Libraries) 目录中。
10835
:::
109-
110-
## III. TypeScript 到原生类型映射
111-
112-
您可以使用以下表格作为参考,了解支持的类型以及它们在每个平台上的映射关系:
113-
114-
### `string`
115-
116-
<VerticalTable data={[
117-
['Nullable Support?', <code>{`string | null`}</code>],
118-
['Android (Java)', <code>String</code>],
119-
['iOS', <code>NSString</code>],
120-
]} />
121-
122-
### `boolean`
123-
124-
<VerticalTable data={[
125-
['Nullable Support?', <code>{`boolean | null`}</code>],
126-
['Android (Java)', <code>Boolean</code>],
127-
['iOS', <code>NSNumber</code>],
128-
]} />
129-
130-
### `number`
131-
132-
<VerticalTable data={[
133-
['Nullable Support?', 'No'],
134-
['Android (Java)', <code>double</code>],
135-
['iOS', <code>NSNumber</code>],
136-
]} />
137-
138-
### Object literal
139-
140-
This is recommended over using plain `Object`, for type safety.
141-
142-
**Example:** `{| foo: string, ... |}`
143-
144-
<VerticalTable data={[
145-
['Nullable Support?', <code>{`{| foo: string, ...|} | null`}</code>],
146-
['Android (Java)', '-'],
147-
['iOS', '-'],
148-
]} />
149-
150-
### `Object`
151-
152-
:::note
153-
Recommended to use [Object literal](#object-literal-1) instead.
154-
:::
155-
156-
<VerticalTable data={[
157-
['Nullable Support?', <code>{`Object | null`}</code>],
158-
['Android (Java)', <code>ReadableMap</code>],
159-
['iOS', <><code>@{}</code> (untyped dictionary)</>],
160-
]} />
161-
162-
### `Array<*>`
163-
164-
<VerticalTable data={[
165-
['Nullable Support?', <code>{`Array<*> | null`}</code>],
166-
['Android (Java)', <code>ReadableArray</code>],
167-
['iOS', <><code>NSArray</code> (or <code>RCTConvertVecToArray</code> when used inside objects)</>],
168-
]} />
169-
170-
### `Function`
171-
172-
<VerticalTable data={[
173-
['Nullable Support?', <code>?{`Function | null`}</code>],
174-
['Android (Java)', '-'],
175-
['iOS', '-'],
176-
]} />
177-
178-
### `Promise<*>`
179-
180-
<VerticalTable data={[
181-
['Nullable Support?', <code>{`Promise<*> | null`}</code>],
182-
['Android (Java)', <code>com.facebook.react.bridge.Promise</code>],
183-
['iOS', <><code>RCTPromiseResolve</code> and <code>RCTPromiseRejectBlock</code></>],
184-
]} />
185-
186-
### Type Unions
187-
188-
Type unions are only supported as callbacks.
189-
190-
**Example:** `'SUCCESS' | 'FAIL'`
191-
192-
<VerticalTable data={[
193-
['Nullable Support?', 'Only as callbacks.'],
194-
['Android (Java)', '-'],
195-
['iOS', '-'],
196-
]} />
197-
198-
### Callbacks
199-
200-
Callback functions are not type checked, and are generalized as `Object`s.
201-
202-
**Example:** `() =>`
203-
204-
<VerticalTable data={[
205-
['Nullable Support?', 'Yes'],
206-
['Android (Java)', <code>com.facebook.react.bridge.Callback</code>],
207-
['iOS', <code>RCTResponseSenderBlock</code>],
208-
]} />
209-
210-
您可能还会发现参考 React Native 中核心模块的 JavaScript 规范很有用。这些规范位于 React Native 存储库的`Libraries/`目录内。
211-
212-
## IV. 在开发过程中调用代码生成
213-
214-
> 本节包含特定于 React Native v0.66 的信息。
215-
216-
通常在构建时调用 Codegen,但是您可能会发现按需生成原生接口代码以进行故障排除很有用。
217-
218-
如果您希望手动调用 codegen,有两个选项:
219-
220-
1. 直接调用 Gradle 任务(Android)。
221-
2. 手动调用脚本。
222-
223-
### Android - 直接调用 Gradle 任务
224-
225-
您可以通过调用以下任务来触发 Codegen:
226-
227-
```bash
228-
./gradlew generateCodegenArtifactsFromSchema --rerun-tasks
229-
```
230-
231-
额外的 `--rerun-tasks` 标志被添加以确保 Gradle 忽略对该任务的 `UP-TO-DATE` 检查。在正常开发过程中,您不应该需要它。
232-
233-
`generateCodegenArtifactsFromSchema` 任务通常在 `preBuild` 任务之前运行,因此您不需要手动调用它,但它将在构建之前触发。
234-
235-
### 手动调用脚本
236-
237-
或者,您可以直接调用 Codegen,绕过 Gradle 插件或 CocoaPods 基础设施。
238-
可以使用以下命令完成此操作。
239-
240-
现在您已经配置了 Gradle 插件或 CocoaPods 库,所以要提供的参数看起来会非常熟悉。
241-
242-
#### 生成模式文件
243-
244-
首先,您需要从 JavaScript 源代码中生成一个模式文件。只有在 JavaScript 规范发生更改时才需要执行此操作。生成此模式的脚本作为`react-native-codegen`包的一部分提供。如果在 React Native 应用程序内运行此脚本,可以直接使用位于`node_modules`目录下的该包:
245-
246-
```bash
247-
node node_modules/react-native-codegen/lib/cli/combine/combine-js-to-schema-cli.js \
248-
<output_file_schema_json> <javascript_sources_dir>
249-
```
250-
251-
> `react-native-codegen`的源代码可在 React Native 存储库中找到,位于`packages/react-native-codegen`目录下。如果需要从源代码构建自己的`react-native-codegen`包,请在该目录中运行 `yarn install``yarn build`. 在大多数情况下,您不需要这样做。
252-
253-
#### 生成原生代码构件
254-
255-
一旦您拥有用于本地模块或组件的架构文件,您可以使用第二个脚本为库生成实际的原生代码构件。您可以使用前一个脚本生成的相同架构文件。
256-
257-
```bash
258-
node node_modules/react-native/scripts/generate-specs-cli.js \
259-
--platform <ios|android> \
260-
--schemaPath <generated_schema_json_file> \
261-
--outputDir <output_dir> \
262-
[--libraryName library_name] \
263-
[--javaPackageName java_package_name] \
264-
[--libraryType all(default)|modules|components]
265-
```
266-
267-
> **注意:** Codegen 的输出构件位于 build 文件夹中,不应提交到版本控制系统。
268-
> 它们仅供参考。
269-
270-
##### 示例
271-
272-
以下是调用 Codegen 脚本的基本示例,用于为提供原生模块的库生成原生 iOS 界面代码。此库的 JavaScript 规范源文件位于`js/`子目录中,而该库的原生代码期望在`ios`子目录中可用原生接口。
273-
274-
```bash
275-
# 生成模式 - 仅在JS规范更改时需要执行
276-
node node_modules/react-native-codegen/lib/cli/combine/combine-js-to-schema-cli.js /tmp/schema.json ./js
277-
278-
# 生成原生代码构件
279-
node node_modules/react-native/scripts/generate-specs-cli.js \
280-
--platform ios \
281-
--schemaPath /tmp/schema.json \
282-
--outputDir ./ios \
283-
--libraryName MyLibSpecs \
284-
--libraryType modules
285-
```
286-
287-
在上面的示例中,代码生成脚本将会生成几个文件:`MyLibSpecs.h``MyLibSpecs-generated.mm`,以及一些 `.h``.cpp` 文件,全部位于 `ios` 目录下。
288-
289-
## V. 关于现有应用的注意事项
290-
291-
本指南提供了迁移基于 React Native 提供的默认应用模板的应用程序的说明。如果您的应用程序与模板有所偏离,或者您正在使用从未基于该模板构建过的应用程序,则以下部分可能会对您有所帮助。
292-
293-
### 查找桥接代理
294-
295-
本指南假设`AppDelegate`被配置为桥接代理。如果您不确定哪个是您的桥接代理,请在`RCTBridge``RCTCxxBridge`中设置断点,运行您的应用,并检查 `self.delegate`.

cndocs/fabric-native-components-android.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,30 @@ title: 'Fabric Native Modules: Android'
55

66
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';
77

8-
Now it's time to write some Android platform code to be able to render the web view. The steps you need to follow are:
8+
现在,是时候编写一些 Android 平台代码,以便能够渲染 web 视图。以下是需要的步骤:
99

10-
- Running Codegen
11-
- Write the code for the `ReactWebView`
12-
- Write the code for the `ReactWebViewManager`
13-
- Write the code for the `ReactWebViewPackage`
14-
- Register the `ReactWebViewPackage` in the application
10+
- 运行 Codegen
11+
- 编写 `ReactWebView` 的代码
12+
- 编写 `ReactWebViewManager` 的代码
13+
- 编写 `ReactWebViewPackage` 的代码
14+
- 在应用中注册 `ReactWebViewPackage`
1515

16-
### 1. Run Codegen through Gradle
16+
### 1. 使用 Gradle 运行 Codegen
1717

18-
Run this once to generate boilerplate that your IDE of choice can use.
18+
运行一次以生成你的 IDE 可以使用的样板代码。
1919

2020
```bash title="Demo/"
2121
cd android
2222
./gradlew generateCodegenArtifactsFromSchema
2323
```
2424

25-
Codegen will generate the `ViewManager` interface you need to implement and the `ViewManager` delegate for the web view.
25+
Codegen 将生成你需要实现 `ViewManager` 接口和 `ViewManager` 委托的 web 视图。
2626

27-
### 2. Write the `ReactWebView`
27+
### 2. 编写 `ReactWebView`
2828

29-
The `ReactWebView` is the component that wraps the Android native view that React Native will render when using our custom Component.
29+
`ReactWebView` 是包装 Android 原生视图的组件,React Native 将在使用自定义组件时渲染它。
3030

31-
Create a `ReactWebView.java` or a `ReactWebView.kt` file in the `android/src/main/java/com/webview` folder with this code:
31+
`android/src/main/java/com/webview` 文件夹中创建一个 `ReactWebView.java``ReactWebView.kt` 文件,并使用以下代码:
3232

3333
<Tabs groupId="android-language" queryString defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
3434
<TabItem value="java">
@@ -183,13 +183,13 @@ class ReactWebView: WebView {
183183
</TabItem>
184184
</Tabs>
185185

186-
The `ReactWebView` extends the Android `WebView` so you can reuse all the properties already defined by the platform with ease.
186+
`ReactWebView` 扩展了 Android `WebView`,因此你可以轻松地重用平台已经定义的所有属性。
187187

188-
The class defines the three Android constructors but defers their actual implementation to the private `configureComponent` function. This function takes care of initializing all the components specific properties: in this case you are setting the layout of the `WebView` and you are defining the `WebClient` that you use to customize the behavior of the `WebView`. In this code, the `ReactWebView` emits an event when the page finishes loading, by implementing the `WebClient`'s `onPageFinished` method.
188+
该类定义了三个 Android 构造函数,但将它们的实际实现推迟到私有 `configureComponent` 函数。此函数负责初始化所有组件的特定属性:在这种情况下,你正在设置 `WebView` 的布局,并定义你用于自定义 `WebView` 行为的 `WebClient`。在此代码中,`ReactWebView` 在页面加载完成后通过实现 `WebClient``onPageFinished` 方法来发出事件。
189189

190-
The code then defines a helper function to actually emit an event. To emit an event, you have to:
190+
然后,代码定义了一个实际发出事件的帮助函数。要发出事件,你必须:
191191

192-
- grab a reference to the `ReactContext`;
192+
- 获取 `ReactContext` 的引用;
193193
- retrieve the `surfaceId` of the view that you are presenting;
194194
- grab a reference to the `eventDispatcher` associated with the view;
195195
- build the payload for the event using a `WritableMap` object;

0 commit comments

Comments
 (0)