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.
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
.)
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