获取按钮的名称onPress in react native [英] Get name of button onPress in react native
本文介绍了获取按钮的名称onPress in react native的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个按钮,它们都调用相同的onPress函数.在回调中,我希望能够区分被按下的位置.
I have two buttons that both call the same onPress function. In the callback I want to be able to differentiate between which was pressed.
<MKRadioButton
title='A'
group={this.radioGroup}
onPress={this._toggle}
/>
<MKRadioButton
title='B'
group={this.radioGroup}
onPress={this._toggle}
/>
然后拨打电话
_toggle(event) {
//what should go here to figure out if title A or B was called?
}
推荐答案
一种解决方案是将该信息作为参数传递:
One solution is to pass that info as a parameter:
<MKRadioButton
title='A'
group={this.radioGroup}
onPress={(event) => this._toggle(event, 'A')}
/>
然后回调将使用该参数
_toggle(event, buttonId) {
// Use buttonId
}
另一个解决方案是始终返回标题prop的父组件:
Another solution is a parent component that always returns the title prop:
class RadioParent extends Component {
render() {
return (
<MKRadioButton
title={this.props.title}
group={this.props.radioGroup}
onPress={(event) => this.props.onPress(event, this.props.title)}
/>
);
}
}
这篇关于获取按钮的名称onPress in react native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文