Skip to content

Auto binding doesn't work with ES6-style components #814

Description

@grgur

When creating a component 'ES6-style' (e.g. class Card extends React.Component), event callbacks will not automatically bind scope.

In the following example, I had to replace
onPress={this.onPress}
with
onPress={this.onPress.bind(this)}.

Using React Native 0.3.11

class Card extends React.Component {
    constructor(properties) {
        super(properties);
        this.state = {
            paired: false,
            visible: false
        };
    }

   onPress() {
        var state = this.state;
        console.log("I am no longer null", state);
    }
    render() {
        return (
          <TouchableHighlight
            onPress={this.onPress.bind(this)}
            underlayColor="blue"
            activeOpacity={0.5}>
            <Text>Dummy Text</Text>
          </TouchableHighlight>
        );
    }
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions