使用一个按钮警告其testID,在本地反应

时间:2017-08-05 10:20:11

标签: javascript react-native

当我按下它时,我希望此按钮提醒其testID:

这是我的代码:

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

它给了我 undefined

我认为解决方案与ref有关。

感谢。

2 个答案:

答案 0 :(得分:0)

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.

答案 1 :(得分:0)

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)}
    />