|
7 | 7 | * @flow
|
8 | 8 | */
|
9 | 9 |
|
| 10 | +import type { GenericStyleProp } from '../../types'; |
| 11 | +import type { ViewProps } from '../../Exports/View'; |
| 12 | + |
10 | 13 | import UIManager from '../../exports/UIManager';
|
11 | 14 | import createDOMProps from '../createDOMProps';
|
12 |
| -import { useMemo, useRef } from 'react'; |
| 15 | +import useStable from '../useStable'; |
| 16 | +import { useRef } from 'react'; |
| 17 | + |
| 18 | +const emptyObject = {}; |
13 | 19 |
|
14 | 20 | function setNativeProps(node, nativeProps, classList, pointerEvents, style, previousStyleRef) {
|
15 | 21 | if (node != null && nativeProps) {
|
@@ -43,22 +49,33 @@ function setNativeProps(node, nativeProps, classList, pointerEvents, style, prev
|
43 | 49 | * Adds non-standard methods to the hode element. This is temporarily until an
|
44 | 50 | * API like `ReactNative.measure(hostRef, callback)` is added to React Native.
|
45 | 51 | */
|
46 |
| -export default function usePlatformMethods(props: Object) { |
| 52 | +export default function usePlatformMethods({ |
| 53 | + classList, |
| 54 | + pointerEvents, |
| 55 | + style |
| 56 | +}: { |
| 57 | + classList?: Array<string | boolean>, |
| 58 | + style?: GenericStyleProp<*>, |
| 59 | + pointerEvents?: $PropertyType<ViewProps, 'pointerEvents'> |
| 60 | +}) { |
47 | 61 | const previousStyleRef = useRef(null);
|
48 |
| - const { classList, style, pointerEvents } = props; |
| 62 | + const setNativePropsArgsRef = useRef(null); |
| 63 | + setNativePropsArgsRef.current = { classList, pointerEvents, style }; |
49 | 64 |
|
50 |
| - return useMemo( |
51 |
| - () => (hostNode: any) => { |
52 |
| - if (hostNode != null) { |
53 |
| - hostNode.measure = callback => UIManager.measure(hostNode, callback); |
54 |
| - hostNode.measureLayout = (relativeToNode, success, failure) => |
55 |
| - UIManager.measureLayout(hostNode, relativeToNode, failure, success); |
56 |
| - hostNode.measureInWindow = callback => UIManager.measureInWindow(hostNode, callback); |
57 |
| - hostNode.setNativeProps = nativeProps => |
58 |
| - setNativeProps(hostNode, nativeProps, classList, pointerEvents, style, previousStyleRef); |
59 |
| - } |
60 |
| - return hostNode; |
61 |
| - }, |
62 |
| - [classList, pointerEvents, style] |
63 |
| - ); |
| 65 | + // Avoid creating a new ref on every render. The props only need to be |
| 66 | + // available to 'setNativeProps' when it is called. |
| 67 | + const ref = useStable(() => (hostNode: any) => { |
| 68 | + if (hostNode != null) { |
| 69 | + hostNode.measure = callback => UIManager.measure(hostNode, callback); |
| 70 | + hostNode.measureLayout = (relativeToNode, success, failure) => |
| 71 | + UIManager.measureLayout(hostNode, relativeToNode, failure, success); |
| 72 | + hostNode.measureInWindow = callback => UIManager.measureInWindow(hostNode, callback); |
| 73 | + hostNode.setNativeProps = nativeProps => { |
| 74 | + const { classList, style, pointerEvents } = setNativePropsArgsRef.current || emptyObject; |
| 75 | + setNativeProps(hostNode, nativeProps, classList, pointerEvents, style, previousStyleRef); |
| 76 | + }; |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + return ref; |
64 | 81 | }
|
0 commit comments