Skip to content

Instantly share code, notes, and snippets.

@ameliabradley
Last active January 14, 2018 00:13
Show Gist options
  • Save ameliabradley/71d4876646d78a47493ce69dd2e1c9eb to your computer and use it in GitHub Desktop.
Save ameliabradley/71d4876646d78a47493ce69dd2e1c9eb to your computer and use it in GitHub Desktop.
Medium - Bound and Unbound Methods
class App extends Component {
constructor() {
super();
this.state = {
clicked: false
};
}
onButtonPress() {
// Will error out if "this" is not accessible
this.setState({ clicked: true });
}
render() {
return (
<View style={styles.app}>
<View style={styles.header}>
<Image accessibilityLabel="React logo" source={logo} resizeMode="contain" style={styles.logo} />
<Text style={styles.title}>Bound Callback Demo</Text>
</View>
<Text style={styles.intro}>
Clicked? {this.state.clicked ? 'Yes' : 'No'}
</Text>
<View style={{ flexDirection: 'row' }}>
<Button onPress={this.onButtonPress} title="Unbound callback will error out" />
<Text> </Text>
<Button onPress={() => this.onButtonPress()} title="Bound callback will work" />
</View>
</View>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment