Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get sender in TouchableHighlight's onPress

Tags:

react-native

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

like image 921
Qing Xu Avatar asked Sep 15 '15 09:09

Qing Xu


People also ask

What is touchablehighlight and how to use it?

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?

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.

How many child components can a touchablehighlight have?

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.

Is there a way to handle touch-based input?

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.


1 Answers

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.

like image 143
Martin Konicek Avatar answered Sep 30 '22 21:09

Martin Konicek