在本机中获取onPress按钮的名称

Ale*_*xis 4 button react-native onpress

我有两个按钮,它们都调用相同的onPress功能.在回调中,我希望能够区分哪个被按下.

<MKRadioButton
   title='A' 
   group={this.radioGroup}
   onPress={this._toggle}
 />

<MKRadioButton
   title='B' 
   group={this.radioGroup}
   onPress={this._toggle}
 />
Run Code Online (Sandbox Code Playgroud)

然后是电话

_toggle(event) {
    //what should go here to figure out if title A or B was called?
}
Run Code Online (Sandbox Code Playgroud)

NiF*_*iFi 8

一种解决方案是将该信息作为参数传递:

<MKRadioButton
  title='A' 
  group={this.radioGroup}
  onPress={(event) => this._toggle(event, 'A')}
/>
Run Code Online (Sandbox Code Playgroud)

然后回调将使用该参数

_toggle(event, buttonId) {
  // Use buttonId
}
Run Code Online (Sandbox Code Playgroud)

编辑:另一个解决方案是始终返回标题道具的父组件:

class RadioParent extends Component {
  render() {
    return (
      <MKRadioButton
        title={this.props.title} 
        group={this.props.radioGroup}
        onPress={(event) => this.props.onPress(event, this.props.title)}
      />
    );
  }
}
Run Code Online (Sandbox Code Playgroud)