I have a list of Views, each View wrapped by TouchableHighlight, I bind a onPress handler for each Touchable
....
<TouchableHighlight onPress={this._preformTouchItem}>
<View style={styles.item}>
<Text>{displayContent}</Text>
</View>
</TouchableHighlight>
....
_preformTouchItem(event) {
}
I debug it in Chrome Dev Tools, the 'event' doesn't bind the 'sender' object, How to know which View is clicked
On Android and iOS, if you tap one button, the opacity of it decreases during the time you pressed it down. TouchableHighlight is used to implement similar effects. It decreases the opacity of the wrapped view on pressed. You will have to play with colors to find out the best combination. Note that it should have only one child.
What is the TouchableHighlight in react native ? TouchableHighlight is a component that is used to provide a wrapper to Views in order to make them respond correctly to touch-based input. On press down the TouchableHighlight component has its opacity decreased which allows the underlying View or other component’s style to get highlighted.
TouchableHighlight must have one child (not zero or more than one). If you wish to have several child components, wrap them in a View. Inherits TouchableWithoutFeedback Props. Determines what the opacity of the wrapped view should be when touch is active.
If you're looking for a more extensive and future-proof way to handle touch-based input, check out the Pressable API. A wrapper for making views respond properly to touches. On press down, the opacity of the wrapped view is decreased, which allows the underlay color to show through, darkening or tinting the view.
In React you would usually not interact with views directly but call setState
and have React re-render the UI for you. For example, you would pass the index of the item:
getInitialState: function() {
var itemToggleStates = [false, false, false];
return itemToggleStates;
},
render: function() {
for (i = 0; i < itemToggleStates.length; i++) {
...
var currentIndex = i;
var itemText = this.state.itemToggleStates[currentIndex] ? 'yes' : 'no';
<TouchableHighlight onPress={() => this._handleItemTouch(currentIndex)}>
<Text>{itemText}</Text>
</TouchableHighlight>
...
}
...
},
_handleItemTouch: function(index) {
var itemToggleStates = this.state.itemToggleStates;
itemToggleStates[index] = !itemToggleStates[index];
setState(itemToggleStates);
}
In rare cases when you really need to access the underlying view, there's a concept called refs
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With