Get name of button onPress in react native

后端 未结 2 1401
南旧
南旧 2021-01-13 07:22

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.



        
2条回答
  •  悲哀的现实
    2021-01-13 07:30

    One solution is to pass that info as a parameter:

     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 (
           this.props.onPress(event, this.props.title)}
          />
        );
      }
    }
    

提交回复
热议问题