I have the following React Native code that runs the press() method when a user taps an image. I want to get the itemIndex prop from the event object. I set a break point in the press method and added some expressions to the Watch. From the Watch I determined that the target (event origination) from the event is the Image which is correct. The itemIndex prop is also available. The element being processed is the currentTarget, the Watch sees it's a "RCTView" and I was expecting a TouchableOpacity, so maybe underneath TouchableOpacity is a View? The currentTarget itemIndex prop is undefined, why? How can I get the props from the currentTarget?
I want to do it this way to avoid creating addition methods for each rendered item.
FYI,
ref={(c) => this._input = c}
will not work because it's being run in a loop.
onPress={(e) => this.press(e, i)}
creates a new function which I'm trying to avoid.
Watch
- target._currentElement.props.itemIndex: 2
- target._currentElement.type.displayName: "RCTImageView"
- currentTarget._currentElement.props.itemIndex: undefined
currentTarget._currentElement.type.displayName: "RCTView"
press: function(event){ var currentTarget = ReactNativeComponentTree.getInstanceFromNode(event.currentTarget); var target = ReactNativeComponentTree.getInstanceFromNode(event.target); var currentTargetIndex = currentTarget._currentElement.props.itemIndex; var targetIndex = target._currentElement.props.itemIndex; var url = this.state.data.items[currentTargetIndex].url; Linking.openURL(url).catch(err => console.error('An error occurred', err)); }, render: function() { return ( <ScrollView horizontal={true} showsHorizontalScrollIndicator={false} style={styles.galleryView}> { this.state.data.items.map((data, i) => <TouchableOpacity itemIndex={i} key={i} activeOpacity={0.5} onPress={this.press} > <Image itemIndex={i} key={i} source={{uri:data.previewImageUri}} style={styles.galleryImage} /> </TouchableOpacity> ) } </ScrollView> ); }