gpt4 book ai didi

javascript - 如何在 onPress 中传递按钮的 id - React Native

转载 作者:行者123 更新时间:2023-11-29 18:45:48 25 4
gpt4 key购买 nike

我正在尝试将按钮的 ID 作为参数传递给由 onPress 事件触发的函数。

setCategory = (e) => {
console.log(e.target.id)
}
render(){
return(
<Button title="News" id={15} onPress={this.setCategory}/>
)
}

所以 15 应该在终端中登录,但我得到的是 undefined。这是我在 Reactjs 中要做的,但我是 React Native 的新手,所以我不知道语法是否错误。

最佳答案

如果 id 来自 Prop ,那么你可以这样做。

<Button title="News" id={this.props.prop_where_id_is} onPress={() => this.setCategory(this.props.prop_where_id_is)} />

然后你的setCategory就变成了

setCategory = (e) => {
console.log(e)
}

关于javascript - 如何在 onPress 中传递按钮的 id - React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54141070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com