我想在 onPress 函数中访问 React Native 元素属性,我尝试使用 event.target 和 ReactNativeComponentTree 但它不起作用,我想在 OnPress 函数中访问 audioPath 属性
<Ionicons
name="ios-play"
size={35}
color={this.state.playAudio ? "red" : "blue"}
style={{
left: 90,
position: "relative",
shadowColor: "#000",
shadowOffset: { width: 0, height: 0 },
shadowOpacity: 0.5,
backgroundColor: "transparent"
}}
audioPath= {audioPath}
onPress={this.audioPathFunction.bind(this)}
/>
最佳答案
<Ionicons
name="ios-play"
size={35}
color={this.state.playAudio ? "red" : "blue"}
style={{
left: 90,
position: "relative",
shadowColor: "#000",
shadowOffset: { width: 0, height: 0 },
shadowOpacity: 0.5,
backgroundColor: "transparent"
}}
audioPath= {audioPath}
onPress={()=> this.audioPathFunction(audioPath)}
/>
视情况而定。你可以在内部访问它,如果它的状态变量/类变量你可以在任何地方访问它,甚至在 onPress 函数内部。如果它仅在 render() 中可用,请使用上面的指针函数(这不是一个好方法)
关于javascript - 如何在 onPress 函数中访问 React Native 标签或元素的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57438975/