Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TouchableOpacity onPress() not working with react-native-modal

Tags:

react-native

I am trying to get TouchableOpacity working with react-native-modal. When I press the button, nothing happens.

Here is my code, on pressing the button, there is no press animation and no alert appears:

<Modal
  isVisible={this.state.modalVisible}
  onBackdropPress={() => this.setState({ modalVisible: false })}
  deviceWidth={Dimensions.get('window').width}
  deviceHeight={Dimensions.get('window').height}
  backdropColor={'rgba(29, 36, 40, 0.5)'}>
  <View style={styles.modal}>
    <Text style={styles.modalTitle}>Test modal</Text>
    <View style={modalButtons}>
      <TouchableOpacity onPress={() => alert('hello!')}><Text style={styles.modalButton}>Test</Text></TouchableOpacity>
      <TouchableOpacity onPress={() => this.setState({ modalVisible: false })}><Text style={styles.modalButton}>Close</Text></TouchableOpacity>
    </View>
  </View>
</Modal>

I am importing TouchableOpacity from react-native, not react-native-gesture-handler (one solution mentioned this; all it did for me was prevent the buttons from being invisible).

EDIT: I've narrowed it down to the View around the buttons. When I remove this the following style, it works:

  modalButtons: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    marginBottom: 30,
  },

Is there a way to keep this flex working? I would like for the buttons to display side by side.

like image 417
Robert Berge Avatar asked Nov 20 '19 18:11

Robert Berge


1 Answers

I had the same problem. Check that your TouchableOpacity is imported from the "react-native" module and not from anything else. (Mine was in react-native-gesture-handler.)

like image 95
Luis Barrientos Fajardo Avatar answered Nov 10 '22 01:11

Luis Barrientos Fajardo