Last active
July 2, 2018 18:25
-
-
Save dmoss18/8c85493ed1917cb7ce289876362b1c94 to your computer and use it in GitHub Desktop.
Better pattern for updating model
This file contains hidden or 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
// This way is needlessly verbose | |
export default class CustomerNameComponent { | |
customer = new Customer() | |
// Annoying boilerplate | |
updateCustomerName = (text: string) => { | |
this.customer.name = text | |
} | |
render() { | |
return ( | |
<TextInput onChangeText={this.updateCustomerName} /> | |
) | |
} | |
} | |
// Instead, use the `updateField` helper so you don't need a new function for every input | |
import { updateField } from 'lib/meta' | |
export default class CustomerNameComponent { | |
customer = new Customer() | |
render() { | |
return ( | |
<TextInput onChangeText={updateField(this.customer, 'name')} /> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment