Skip to content

Commit 9202fc5

Browse files
committed
Remove findNodeHandle and measure*() New Architecture Migration Steps
1 parent 170430f commit 9202fc5

File tree

1 file changed

+21
-142
lines changed

1 file changed

+21
-142
lines changed

docs/new-architecture-library-intro.md

Lines changed: 21 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -220,159 +220,38 @@ Codegen can be configured in the `package.json` file of your Library. Add the fo
220220
221221
Android also requires to have the [React Gradle Plugin properly configured](new-architecture-app-intro#android-specifics) in your app.
222222
223-
## Preparing your JavaScript Codebase for the new React Native Renderer (Fabric)
223+
## Migrating from `UIManager` JavaScript APIs
224224
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!
225+
In the New Architecture, most `UIManager` methods will become available as instance methods on native component instances obtained via `ref`:
226226
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-
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-
231-
1. Migrating findNodeHandle / getting a HostComponent
232-
2. Migrating `.measure*()`
233-
3. Migrating off `setNativeProps`
234-
4. Move the call to `requireNativeComponent` to a separate file
235-
5. Migrating off `dispatchViewManagerCommand`
236-
6. Creating NativeCommands with `codegenNativeCommands`
237-
238-
### Migrating `findNodeHandle` / getting a `HostComponent`
239-
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-
242-
As a concrete example, this code uses `findNodeHandle` to tunnel from `ParentComponent` through to the `View` rendered by `ChildComponent`.
243-
244-
```tsx
245-
class ParentComponent extends React.Component<Props> {
246-
_ref?: React.ElementRef<typeof ChildComponent>;
247-
248-
render() {
249-
return <ChildComponent ref={this._captureRef} onSubmit={this._onSubmit} />
250-
}
251-
252-
_captureRef: (ref: React.ElementRef<typeof ChildComponent>) => {
253-
this._ref = ref;
254-
}
255-
256-
_onSubmit: () => {
257-
const nodeHandle = findNodeHandle(this._ref);
258-
259-
if (nodeHandle) {
260-
UIManager.measure(nodeHandle, () => {});
261-
}
262-
}
263-
}
264-
265-
class ChildComponent extends React.Component<Props> {
266-
render() {
267-
return (
268-
<View>
269-
<SubmitButton onSubmit={props.onSubmit} />
270-
</View>
271-
);
272-
}
273-
}
274-
```
275-
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-
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-
280-
#### Using `forwardRef`
281-
282-
```tsx
283-
class ParentComponent extends React.Component<Props> {
284-
_ref?: React.ElementRef<typeof ChildComponent>;
285-
286-
render() {
287-
return <ChildComponent ref={this._captureRef} onSubmit={this._onSubmit} />
288-
}
289-
290-
_captureRef: (ref: React.ElementRef<typeof ChildComponent>) => {
291-
this._ref = ref;
292-
}
293-
294-
_onSubmit: () => {
295-
if (this._ref != null)
296-
this._ref.measure(() => {});
297-
}
298-
}
299-
}
300-
301-
const ChildComponent = React.forwardRef((props, forwardedRef) => {
302-
return (
303-
<View ref={forwardedRef}>
304-
<SubmitButton onSubmit={props.onSubmit} />
305-
</View>
306-
);
307-
});
308-
```
309-
310-
#### Using a getter, (note the addition of `getViewRef`)
311-
312-
```tsx
313-
class ParentComponent extends React.Component<Props> {
314-
_ref?: React.ElementRef<typeof ChildComponent>;
315-
316-
render() {
317-
return <ChildComponent ref={this._captureRef} onSubmit={this._onSubmit} />
318-
}
319-
320-
_captureRef: (ref: React.ElementRef<typeof ChildComponent>) => {
321-
this._ref = ref;
322-
}
323-
324-
_onSubmit: () => {
325-
if (this._ref != null)
326-
this._ref.getViewRef().measure(() => {});
327-
}
328-
}
329-
}
330-
331-
class ChildComponent extends React.Component<Props> {
332-
_ref?: React.ElementRef<typeof View>;
333-
334-
render() {
335-
return (
336-
<View ref={this._captureRef}>
337-
<SubmitButton onSubmit={props.onSubmit} />
338-
</View>
339-
);
340-
}
227+
```ts
228+
function MyComponent(props: Props) {
229+
const viewRef = useRef(null);
341230

342-
getViewRef(): ?React.ElementRef<typeof View> {
343-
return this._ref;
344-
}
231+
useEffect(() => {
232+
viewRef.measure(((left, top, width, height, pageX, pageY) => {
233+
// ...
234+
});
235+
}, []);
345236

346-
_captureRef: (ref: React.ElementRef<typeof View>) => {
347-
this._ref = ref;
348-
}
237+
return <View ref={viewRef} />;
349238
}
350239
```
351240
352-
### Migrating `.measure*()`
241+
This new API design provides several benefits:
353242
354-
Lets take a look at an example calling `UIManager.measure`. This code might look something like this
243+
- Better developer ergonomics by removing the need for separately importing `UIManager` or calling `findNodeHandle`.
244+
- Better performance by avoiding the node handle lookup step.
245+
- Directionally aligned with [the analogous deprecation of `findDOMNode`](https://reactjs.org/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage).
355246
356-
```js
357-
const viewRef: React.ElementRef<typeof View> = /* ... */;
358-
const viewHandle = ReactNative.findNodeHandle(viewRef);
359-
360-
UIManager.measure(viewHandle, (x, y, width, height) => {
361-
// Use layout metrics.
362-
});
363-
```
364-
365-
In order to call `UIManager.measure*` we need to call `findNodeHandle` first and pass in those handles. With the new API, we instead call `measure` directly on native refs without `findNodeHandle`. The example above with the new API looks like this:
247+
We will eventually deprecate `UIManager`. However, we recognize that migrations demand a high cost for many application and library authors. In order to minimize this cost, we plan to continuing supporting as many of the methods on `UIManager` as possible in the New Architecture.
366248
367-
```js
368-
const viewRef: React.ElementRef<typeof View> = /* ... */;
369-
370-
viewRef.measure((x, y, width, height) => {
371-
// Use layout metrics.
372-
});
373-
```
249+
**Support for `UIManager` methods in the New Architecture is actively being developed.** While we make progress here, early adopters can still experiment with the New Architecture by following these steps to migrate off common `UIManager` APIs:
374250
375-
`findNodeHandle` can be called with any component as an argument, but the new `.measure*` can only be called on native refs. If the ref originally passed into `findNodeHandle` is not a native ref to start with, use the strategies above in _getting a HostComponent_ to find the native ref.
251+
1. Migrating off `setNativeProps`
252+
2. Move the call to `requireNativeComponent` to a separate file
253+
3. Migrating off `dispatchViewManagerCommand`
254+
4. Creating NativeCommands with `codegenNativeCommands`
376255
377256
### Migrating off `setNativeProps`
378257

0 commit comments

Comments
 (0)