Skip to content

Instantly share code, notes, and snippets.

@peterpme
Last active August 26, 2016 15:49
Show Gist options
  • Save peterpme/e8729252d915374f26ddeac08913e6a5 to your computer and use it in GitHub Desktop.
Save peterpme/e8729252d915374f26ddeac08913e6a5 to your computer and use it in GitHub Desktop.
How can I pass both the key and the value without resorting to bind?
const handleFieldUpdate = (key) => (value) => setState({ [key]: value })
const SomeForm = () => (
<Input
placeholder='Name'
onChange={handleFieldUpdate('name')}
/>
)
const Input = ({
value,
placeholder,
onChange,
}) => (
<TextInput // React Native
value={value}
placeholder={placeholder}
onChangeText={onChange}
/>
)
function handleFieldUpdate(key, value) {
this.setState({
[key]: value,
});
}
const SomeForm = () => (
<Input
placeholder='Name'
onChange={handleFieldUpdate}
/>
)
const Input = ({
value,
placeholder,
onChange,
}) => (
<TextInput // React Native
value={value}
placeholder={placeholder}
onChangeText={onChange}
/>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment