Home > Back-end >  How to get button title?
How to get button title?

Time:11-24

Im trying to get my button name , from the event, but I'm not finding it

my button call(JSX) code:

   {...}
 <View style={styles.container}> 
    {userForm.map((item) => (       
      <Button title = {item.name.slice(7)} key = {item.info.uuid} onPress = {handleClick} />    
    ))}
    
    </View>
      {...}

My handle click constant where I try to get the button title:

  const handleClick = (e) => {
    var objeto = e.currentTarget.title;
    console.log(objeto);

    };

someone knows where or how to get my button title ?

CodePudding user response:

e.currentTarget gives you the button element that is rendered in your screen. You could access its text content by using e.currentTarget.textContent or you can pass the title as an argument directly to your handleClick function:

<Button title = {item.name.slice(7)} key = {item.info.uuid} onPress = {handleClick(item.name.slice(7))} />


  const handleClick = (buttonTitle) => (e) => {
    console.log(buttonTitle)
    };

CodePudding user response:

Try changing your objeto to var objeto = e.currentTarget.title;

You're setting title on the component but trying to read name on your handleClick.

  • Related