0

I have two buttons like this:

<TouchableOpacity onPress={() => this.setModalVisible(true)} style={stylesDropDown.acceptButton}> <Text> Accept </Text> </TouchableOpacity> <TouchableOpacity onPress={() => this.setModalVisible(true)} style=stylesDropDown.refuseButton}> <Text> Refuse </Text> </TouchableOpacity> 

I want that when the 'setModalVisible' function is executed to know which of these two buttons has been pressed. Any ideas?

1
  • 1
    Have a new state called buttonPressed, onPress of either of those update the state to the respective string. Commented Dec 14, 2020 at 10:06

4 Answers 4

1

You could try something like:

 const handleBtn = (e) =>{ setModalVisible(true) console.log(e.target.id) } 

and in the DOM:

<TouchableOpacity id="btn_1" onPress={handleBtn} style=stylesDropDown.refuseButton}> <Text> Rechazar </Text> </TouchableOpacity> 
Sign up to request clarification or add additional context in comments.

Comments

0

I can't comment because of low score but try:

onPress={event => { const whoAmI = event.target; //Change to what you want to get from clicked element this.setModalVisible(true) }} 

Comments

0

You an console.log the button which is pressed,

here is how:

 <TouchableOpacity onPress={() => { setModalVisible(true); console.log('Aceptar Clicked'); }} style={stylesDropDown.acceptButton}> <Text> Aceptar </Text> </TouchableOpacity> <TouchableOpacity onPress={() => { setModalVisible(true); console.log('Rechazar Clicked'); }} style={stylesDropDown.refuseButton}> <Text> Rechazar </Text> </TouchableOpacity> 

Comments

0

just make it in 2 function

pressBtn1 =()=>{ setModalVisible(true) } pressBtn2 =()=>{ setModalVisible(true) } 

and in the dom:

<TouchableOpacity onPress={pressBtn1} style={stylesDropDown.acceptButton}> <Text> Accept </Text> </TouchableOpacity> <TouchableOpacity onPress={pressBtn2} style=stylesDropDown.refuseButton}> <Text> Refuse </Text> </TouchableOpacity> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.