|
1 | | -import React, { Ref, RefObject, useEffect, useRef } from 'react'; |
| 1 | +import React, { Ref, RefObject, useEffect, useMemo, useRef } from 'react'; |
2 | 2 | import RNGestureHandlerModule from '../../RNGestureHandlerModule.web'; |
3 | 3 | import { ActionType } from '../../ActionType'; |
4 | 4 | import { PropsRef } from '../../web/interfaces'; |
@@ -27,6 +27,8 @@ const EMPTY_HANDLERS = new Set<number>(); |
27 | 27 | const HostGestureDetector = (props: GestureHandlerDetectorProps) => { |
28 | 28 | const { handlerTags, children } = props; |
29 | 29 |
|
| 30 | + const handlerTagsSet = useMemo(() => new Set(handlerTags), [...handlerTags]); |
| 31 | + |
30 | 32 | const viewRef = useRef<Element>(null); |
31 | 33 | const propsRef = useRef<GestureHandlerDetectorProps>(props); |
32 | 34 | const attachedHandlers = useRef<Set<number>>(new Set<number>()); |
@@ -110,25 +112,31 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => { |
110 | 112 | tagMessage('Detector expected to have exactly one child element') |
111 | 113 | ); |
112 | 114 | } |
| 115 | + }, [children]); |
113 | 116 |
|
114 | | - const currentHandlerTags = new Set(handlerTags); |
115 | | - detachHandlers(currentHandlerTags, attachedHandlers.current); |
| 117 | + useEffect(() => { |
| 118 | + if (React.Children.count(children) !== 1) { |
| 119 | + throw new Error( |
| 120 | + tagMessage('Detector expected to have exactly one child element') |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + detachHandlers(handlerTagsSet, attachedHandlers.current); |
116 | 125 |
|
117 | 126 | attachHandlers( |
118 | 127 | viewRef, |
119 | 128 | propsRef, |
120 | | - currentHandlerTags, |
| 129 | + handlerTagsSet, |
121 | 130 | attachedHandlers.current, |
122 | 131 | ActionType.NATIVE_DETECTOR |
123 | 132 | ); |
124 | | - |
125 | 133 | return () => { |
126 | 134 | detachHandlers(EMPTY_HANDLERS, attachedHandlers.current); |
127 | 135 | attachedVirtualHandlers?.current.forEach((childHandlerTags) => { |
128 | 136 | detachHandlers(EMPTY_HANDLERS, childHandlerTags); |
129 | 137 | }); |
130 | 138 | }; |
131 | | - }, [handlerTags, children]); |
| 139 | + }, [handlerTagsSet, viewRef]); |
132 | 140 |
|
133 | 141 | useEffect(() => { |
134 | 142 | const virtualChildrenToDetach: Set<number> = new Set( |
|
0 commit comments