Last active
January 14, 2018 00:13
-
-
Save ameliabradley/71d4876646d78a47493ce69dd2e1c9eb to your computer and use it in GitHub Desktop.
Medium - Bound and Unbound Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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