react-native onPress binding with an argument

前端 未结 6 2107
予麋鹿
予麋鹿 2020-12-24 04:42

The desired behaviour is to pass an argument (text) to the onClick handler to console.log it but it seems that I\'m doing something wrong with the syntax.

If I leave

6条回答
  •  温柔的废话
    2020-12-24 05:13

    You can do the binding in the constructor by using ES6:

    export default class Nav extends Component {
      constructor(props) {
        super(props);
    
        this.onPress = this.onPress.bind(this);
      }
    

    and then

      onPress(txt) {
        console.log(txt);
      }
    
      render() {
        return (
          
            ####################
            Intro Screen
            Number: {this.props.numbers}
             this.onPress('foo')}>
              Go to Foo
            
          
        );
      }
    }
    

提交回复
热议问题