Amin Meyghani Amin Meyghani - 7 months ago 41
Javascript Question

Using a single handler function to set the state of inputs in a form in React

I'm new to React and I can't find an easy way to create a single handler function to set the state of two inputs in a form. This is what I have so far:

<form onSubmit="onSave">
<input type="text" value={} onChange={this.setValue('name')}>
<input type="text" value={ onChange={this.setValue('bio')}>


getInitialState() {
return {
name: '',
bio: '',
setValue: function(key) {
const self = this;
return function(e) {
var o = {};
o[key] =;
onSave() {

The issue that I have is that only one input is updated. Also I'm aware of the form components out there, but I really want to know if something like this is possible, because my use case is very simple.

ray ray

If I understand the question, you could give your inputs a name and then do setState({[]:}).