Skip to content

Instantly share code, notes, and snippets.

@dmoss18
Last active Jul 2, 2018
Embed
What would you like to do?
Better pattern for updating model
// 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