Get name of button onPress in react native

后端 未结 2 1426
南旧
南旧 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:31

    To improve performance, you can bind the event handler in the constructor to have it rendered only once

    Facebook tip (at the bottom of the page)

    We generally recommend binding in the constructor or using the property initializer syntax, to avoid this sort of performance problem.

    class MKRadioButtonWrapper extends React.PureComponent {
      constructor(props) {
        super(props);
        this.buttonPressed = this.buttonPressed.bind(this);
      }
    
      buttonPressed(){
        this.props.onPress(this.props.title);
      }
    
      render() {
        return (
        
        );
      }
    }
    
    class App extends React.Component {
      constructor(props) {
        super(props);
    
        this._toggle = this._toggle.bind(this);
      }
    
      _toggle(title) {
        //do what you want with the title
      }
    
      render() {
        return (
          
            
    
            
          
        );
      }
    }
    

提交回复
热议问题