Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Last active September 20, 2018 19:06
Show Gist options
  • Select an option

  • Save ajcrites/638e0ce2e4bcc6a0923ae66224318c7a to your computer and use it in GitHub Desktop.

Select an option

Save ajcrites/638e0ce2e4bcc6a0923ae66224318c7a to your computer and use it in GitHub Desktop.
// Bad Example
export class ReferenceLiteralComponent extends React.Component {
doSomething() {}
render() {
return (
<View style={{ backgroundColor: 'red' }}>{
[1, 2, 3].map(
num => <Text onPress={() => this.doSomething()}>{num}: {new Date()}</Text>
)
}</View>
);
}
}
// Good Example
const styles = StyleSheet.create({
view: { backgroundColor: 'red' },
});
export class NoReferenceLiteralComponent extends React.Component {
// Instance-bound function allows us to still use `this`
// without an additional binding which would create a new function
doSomething = () => {}
numbers = [1, 2, 3];
date = new Date();
render() {
return (
<View style={styles.view}>{
this.numbers.map(
num => <Text onPress={this.doSomething}>{num}: {this.date}</Text>
)
}</View>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment