You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/new-architecture-intro.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ import NewArchitectureWarning from './\_markdown-new-architecture-warning.mdx';
9
9
10
10
# Getting Started with the New Architecture
11
11
12
-
This migration guide is designed for React Native **library authors** and **application developers**. It outlines the steps you need to follow to roll out the new Architecture, composed by the **New Native Module system (Turbo Module) and the new Renderer (Fabric)** to your **Android** and **iOS** libraries and apps.
12
+
This migration guide is designed for React Native **library authors** and **application developers**. It outlines the steps you need to follow to roll out the new Architecture, composed of the **new NativeModule system (Turbo Module) and the new Renderer (Fabric)** to your **Android** and **iOS** libraries and apps.
Copy file name to clipboardExpand all lines: docs/new-architecture-library-intro.md
+19-19Lines changed: 19 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -14,20 +14,20 @@ The following steps will help ensure your modules and components are ready for t
14
14
15
15
## Define Specs in JavaScript
16
16
17
-
The JavaScript specs serve as the source of truth for the methods that are provided by each native module. They define all APIs that are provided by the native module, along with the types of those constants and functions.
17
+
The JavaScript specs serve as the source of truth for the methods provided by each native module. They define all APIs provided by the native module, along with the types of those constants and functions.
18
18
Using a **typed** spec file allows you to be intentional and declare all the input arguments and outputs of your native module’s methods.
19
19
20
20
:::info
21
21
**TypeScript** support is in beta right now.
22
22
:::
23
23
24
-
To adopt the New Architecture, you start by creating these specs for your native modules and native components. You can do this prior to actually migrating to the New Architecture: the specs will be used later on to generate native interface code for all the supported platforms as a way to enforce uniform APIs across platforms.
24
+
To adopt the New Architecture, you start by creating these specs for your native modules and native components. You can do this before migrating to the New Architecture: the specs will be used later on to generate native interface code for all the supported platforms as a way to enforce uniform APIs across platforms.
25
25
26
26
#### Turbo Native Modules
27
27
28
-
JavaScript spec files **must** be named `Native<MODULE_NAME>.js` and they export a `TurboModuleRegistry``Spec` object. The name convention is important because the Codegen process looks for modules whose `js` (`jsx`, `ts`, or `tsx`) spec file starts with the keyword `Native`.
28
+
JavaScript spec files **must** be named `Native<MODULE_NAME>.js`, and they export a `TurboModuleRegistry``Spec` object. The name convention is important because the Codegen process looks for modules whose `js` (`jsx`, `ts`, or `tsx`) spec file starts with the keyword `Native`.
29
29
30
-
The following is a basic JavaScript spec template, written using the [Flow](https://flow.org/)syntax as well as [TypeScript](https://www.typescriptlang.org/).
30
+
The following is a basic JavaScript spec template, written using the [Flow](https://flow.org/)and [TypeScript](https://www.typescriptlang.org/) syntax.
When using Flow or TypeScript, you will be using [type annotations](https://flow.org/en/docs/types/) to define your spec. Keeping in mind that the goal of defining a JavaScript spec is to ensure the generated native interface code is typesafe, the set of supported types will be those that can be mapped one-to-one to a corresponding type on the native platform.
123
+
When using Flow or TypeScript, you will be using [type annotations](https://flow.org/en/docs/types/) to define your spec. Keeping in mind that the goal of defining a JavaScript spec is to ensure the generated native interface code is type-safe, the set of supported types will be those that can be mapped one-to-one to a corresponding type on the native platform.
124
124
125
125
<!-- alex ignore primitive -->
126
126
127
-
In general, this means you can use primitive types (strings, numbers, booleans), and function types, object types, and array types. Union types, on the other hand, are not supported. All types must be read-only. For Flow: either `+` or `$ReadOnly<>` or `{||}` objects. For TypeScript: `readonly` for properties, `Readonly<>` for objects, and `ReadonlyArray<>` for arrays.
127
+
In general, this means you can use primitive types (strings, numbers, booleans), function types, object types, and array types. Union types, on the other hand, are not supported. All types must be read-only. For Flow: either `+` or `$ReadOnly<>` or `{||}` objects. For TypeScript: `readonly` for properties, `Readonly<>` for objects, and `ReadonlyArray<>` for arrays.
128
128
129
129
> See Appendix [II. Flow Type to Native Type Mapping](new-architecture-appendix#ii-flow-type-to-native-type-mapping).
130
130
> See Appendix [III. TypeScript to Native Type Mapping](new-architecture-appendix#iii-typescript-to-native-type-mapping).
@@ -144,9 +144,9 @@ Later on those types are compiled to coresponding equivalents on target platform
144
144
145
145
### Be Consistent Across Platforms and Eliminate Type Ambiguity
146
146
147
-
Before adopting the New Architecture in your native module, you will need to ensure your methods are consistent across platforms. This is something you will realize as you set out to write the JavaScript spec for your native module - remember that JavaScript spec defines what the methods will look like on all supported platforms.
147
+
Before adopting the New Architecture in your native module, you should ensure your methods are consistent across platforms. You will realize this as you set out to write the JavaScript spec for your native module - remember that JavaScript spec defines what the methods will look like on all supported platforms.
148
148
149
-
If your existing native module has methods with the same name on multiple platforms but with different numbers or types of arguments across platforms, you will need to find a way to make these consistent. If you have methods that can take two or more different types for the same argument, you will also need to find a way to resolve this type of ambiguity as type unions are intentionally not supported.
149
+
If your existing native module has methods with the same name on multiple platforms, but with different numbers or types of arguments across platforms, you will need to find a way to make these consistent. If you have methods that can take two or more different types for the same argument, then you need to find a way to resolve this type of ambiguity as type unions are intentionally not supported.
150
150
151
151
## Make Sure _autolinking_ is Enabled
152
152
@@ -158,7 +158,7 @@ If your existing native module has methods with the same name on multiple platfo
158
158
159
159
On Android, this generally requires you to include `native_modules.gradle` in both your `settings.gradle[.kts]` and `build.gradle[.kts]`.
160
160
161
-
If you used the default template provided with React Native (i.e. you used `yarnreact-nativeinit <Project>`), then you have autolinking already enabled.
161
+
If you used the default template provided with React Native (i.e., `yarnreact-nativeinit <Project>`), then autolinking is already enabled.
162
162
163
163
You can anyway verify that you have it enabled with:
164
164
@@ -214,17 +214,17 @@ Codegen can be configured in the `package.json` file of your Library. Add the fo
214
214
215
215
- The `codegenConfig` is the key used by the Codegen to verify that there is some code to generate.
216
216
- The `name` field is the name of the library.
217
-
- The `type` field is used to identify the type of module we want to create. Our suggestion is to keep`all` to support libraries that contain both Turbo Native Module and Fabric Native Components.
217
+
- The `type` field is used to identify the type of module we want to create. We suggest keeping`all` to support libraries that contain both Turbo Native Module and Fabric Native Components.
218
218
- The `jsSrcsDir` is the directory where the codegen will start looking for JavaScript specs.
219
-
- The `android.javaPackageName` is the name of the package where the generated code wil end up.
219
+
- The `android.javaPackageName` is the name of the package where the generated code ends up.
220
220
221
221
Android also requires to have the [React Gradle Plugin properly configured](new-architecture-app-intro#android-specifics) in your app.
222
222
223
223
## Preparing your JavaScript Codebase for the new React Native Renderer (Fabric)
224
224
225
225
The new renderer, Fabric, doesn’t use the UIManager, so direct calls to UIManager will need to be migrated. Historically, calls to UIManager had some pretty complicated patterns. Fortunately, we’ve created new APIs that are much cleaner. These new APIs are forward compatible with Fabric, so you can migrate your code today, and the APIs will work properly when you turn on Fabric!
226
226
227
-
Fabric will be providing new typesafe JS APIs that are much more ergonomic than some of the patterns we've seen in product code today. These APIs require references to the underlying component, no longer using the result of `findNodeHandle`. `findNodeHandle` is used to search the tree for a native component given a class instance. This was breaking the React abstraction model. `findNodeHandle` is not compatible with React 18. Deprecation of `findNodeHandle` in React Native is similar to the [deprecation of `findDOMNode` in React DOM](https://reactjs.org/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage).
227
+
Fabric will be providing new type-safe JS APIs that are much more ergonomic than some of the patterns we've seen in product code today. These APIs require references to the underlying component, no longer using the result of `findNodeHandle`. `findNodeHandle` is used to search the tree for a native component given a class instance. This was breaking the React abstraction model. `findNodeHandle` is not compatible with React 18. Deprecation of `findNodeHandle` in React Native is similar to the [deprecation of `findDOMNode` in React DOM](https://reactjs.org/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage).
228
228
229
229
While we know that all deprecations are a hassle, this guide is intended to help people update components as smoothly as possible. Here are the steps you need to take to get your JS codebase ready for Fabric:
230
230
@@ -237,7 +237,7 @@ While we know that all deprecations are a hassle, this guide is intended to help
237
237
238
238
### Migrating `findNodeHandle` / getting a `HostComponent`
239
239
240
-
Much of the migration work requires a HostComponent ref to access certain APIs that are only attached to host components (like View, Text, or ScrollView). HostComponents are the return value of calls to `requireNativeComponent`. `findNodeHandle` tunnels through multiple levels of component hierarchy to find the nearest native component.
240
+
Most of the migration work requires a HostComponent ref to access certain APIs that are only attached to host components (like View, Text, or ScrollView). HostComponents are the return value of calls to `requireNativeComponent`. `findNodeHandle` tunnels through multiple levels of component hierarchy to find the nearest native component.
241
241
242
242
As a concrete example, this code uses `findNodeHandle` to tunnel from `ParentComponent` through to the `View` rendered by `ChildComponent`.
243
243
@@ -275,7 +275,7 @@ class ChildComponent extends React.Component<Props> {
275
275
276
276
We can’t convert this call to `this._ref.measure` because `this._ref` is an instance to `ChildComponent`, which is not a HostComponent and thus does not have a `measure` function.
277
277
278
-
`ChildComponent` renders a `View`, which is a HostComponent, so we need to get a reference to `View` instead. There are typically two approaches to get what we need. If the component we need to get the ref from is a function component, using `forwardRef` is probably the right choice. If it is a class component with other public methods, adding a public method for getting the ref is an option. Here are examples of those two forms:
278
+
`ChildComponent` renders a `View`, which is a HostComponent, so we need to get a reference to `View` instead. There are typically two approaches to getting what we need. If the component we need to get the ref from is a function component, using `forwardRef` is probably the right choice. If it is a class component with other public methods, adding a public method for getting the ref is an option. Here are examples of those two forms:
279
279
280
280
#### Using `forwardRef`
281
281
@@ -429,17 +429,17 @@ The resulting component can be represented as such:
429
429
/>
430
430
```
431
431
432
-
Note that all prop values set in the render function are unchanged even though `setNativeProps` didn’t pass those props. Also, `style` is now the merged value of its value prior to `_onSubmit` and `styles.submittedView`. This is the important takeaway: in our current pre-Fabric world, **component props persist.** The platform view caches the prop values its passed from the JS side. If this wasn’t the case then following the setNativeProps call, React Native would have rendered a component like this:
432
+
Note that all prop values set in the render function are unchanged even though `setNativeProps` didn’t pass those props. Also, `style` is now the merged value of its value prior to `_onSubmit` and `styles.submittedView`. This is the important takeaway: in our current pre-Fabric world, **component props persist.** The platform view caches the prop values it's passed from the JS side. If this wasn’t the case, then following the setNativeProps call, React Native would have rendered a component like this:
The fact that React Native stores some internal state of each component that isn’t explicitly declared in last render is what Fabric intends to fix.
438
+
The fact that React Native stores some internal state of each component that isn’t explicitly declared in the last render is what Fabric intends to fix.
439
439
440
440
#### Moving `setNativeProps` to state
441
441
442
-
Taking those caveats into account, a proper migration would look like this:
442
+
Taking the above caveats into account, a proper migration would look like this:
443
443
444
444
```tsx
445
445
class MyComponent extends React.Component<Props> {
- We are using the `hasSubmitted` flag to represent whether or not we want to apply `styles.submittedView`. If the style was dynamic, then it makes sense to store the style object in state
475
-
- `accessibility` is now explicitly passed to the View component as a boolean. This differs from the prior implementation where `accessibility` wasn’t passed as a prop in the initial render, but in this case, we know the non-specification of `accessibility` is handled in the same way as `accessibilty={false}`
474
+
- We are using the `hasSubmitted` flag to represent whether or not we want to apply `styles.submittedView`. If the style was dynamic, then it makes sense to store the style object in state.
475
+
- `accessibility` is now explicitly passed to the View component as a boolean. This differs from the prior implementation where `accessibility` wasn’t passed as a prop in the initial render, but in this case, we know the non-specification of `accessibility` is handled in the same way as `accessibilty={false}`.
476
476
477
477
Be wary of your assumptions, as uncaught subtleties can introduce differences in behavior! It’s a good idea to have snapshot tests of your component as they will highlight any differences pre and post your migration.
Copy file name to clipboardExpand all lines: docs/new-architecture-library-ios.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ If you need to explicitly know which `folly_flags` React Native is using, you ca
34
34
35
35
The JavaScript spec for your native module or component will be used to generate native interface code for each supported platform (i.e., Android and iOS). These native interface files will be generated when a React Native application that depends on your library is built.
36
36
37
-
While this generated native interface code **will not ship as part of your library**, you do need to make sure your Objective-C or Java code conforms to the protocols provided by these native interface files. You can use the Codegen script to generate your library’s native interface code in order to use it **as reference**.
37
+
While this generated native interface code **will not ship as part of your library**, you do need to make sure your Objective-C or Java code conforms to the protocols provided by these native interface files. You can use the Codegen script to generate your library’s native interface code in order to use it **as a reference**.
0 commit comments