Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of button onPress in react native

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}
 />

then the call

_toggle(event) {
    //what should go here to figure out if title A or B was called?
}
like image 376
Alexis Avatar asked Jun 07 '17 21:06

Alexis


1 Answers

One solution is to pass that info as a parameter:

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

The callback would then use that parameter

_toggle(event, buttonId) {
  // Use buttonId
}

EDIT: 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)}
      />
    );
  }
}
like image 141
NiFi Avatar answered Oct 06 '22 03:10

NiFi