Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在react native中,使按钮向其testID发出警报_Javascript_React Native - Fatal编程技术网

Javascript 在react native中,使按钮向其testID发出警报

Javascript 在react native中,使按钮向其testID发出警报,javascript,react-native,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(te

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

这是我的密码:

<Button
   testID = "buttttt"
   ref    = {(Button) => {b = Button;}}
   title  = "Learn More"
   color  = "#841584"
   accessibilityLabel = "Learn more about this purple button"
   onPress = {() => alert(testID)}
 />
{b=Button;}
title=“了解更多信息”
color=“#841584”
accessibilityLabel=“了解有关此紫色按钮的详细信息”
onPress={()=>alert(testID)}
/>
它给了我未定义的

我认为解决方法与ref有关


谢谢。

您必须使用
这个.getAttribute(“testID”)


您应该使用
数据testid
作为属性名。这是更好的样式,您也可以通过
this.dataset.testid
访问它

我认为您应该在onPress处理程序中执行
this.b.props.testId
而不是
this.testId
。大概是这样的:

    <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)}
    />
{this.b=b;}
title=“了解更多信息”
color=“#841584”
accessibilityLabel=“了解有关此紫色按钮的详细信息”
onPress={()=>alert(this.b.props.testID)}
/>

我写了:onPress={()=>alert(this.getAttribute(“testID”))}。它给了我一个错误:undefined不是一个函数(计算“\u this3.getAttribute(“testID”)”)使用普通JavaScript:
onclick=“alert(this.getAttribute(“testID”)”)”
。这应该行得通,但行不通。我认为,因为onPress道具是在react原生按钮JSX中定义的。所以它不是简单的javaScript。所以什么也没有发生,因为onPress道具负责处理点击。请提供有关错误的更多信息或显示整个错误数据。你想在这里检查按钮或警报的工作情况吗?它只是警报:未定义。我尝试了不同的方法,在你能找到的答案中。@bashirabdelwah尝试了
这个.b.props.testID
,它对我有效。什么是
警报
功能?尝试
onPress={()=>{Alert.Alert(“Title”,this.b.props.testID)}/>
-您可以通过@DhruvParmar working看到一个使用答案的示例。