简体   繁体   中英

make a button alert its testID, in react-native

I want this Button to alert its testID, when I press it:

here is my code:

<Button
   testID = "buttttt"
   ref    = {(Button) => {b = Button;}}
   title  = "Learn More"
   color  = "#841584"
   accessibilityLabel = "Learn more about this purple button"
   onPress = {() => alert(testID)}
 />

Its giving me undefined .

I think the solution has to do with ref.

Thanks.

You have to use this.getAttribute("testID") .

And you should use data-testid as attribute name. That's better style and you can access it via this.dataset.testid too.

I think you should be doing this.b.props.testId instead of this.testId in your onPress handler. Something like this:

    <Button
      testID="buttttt"
      ref={(b)=>{this.b = b;}}
      title="Learn More"
      color="#841584"
      accessibilityLabel="Learn more about this purple button"
      onPress={()=>alert(this.b.props.testID)}
    />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM