EDIT: this is a duplicate, see here
I can't find any examples of using a dynamic key name when setting the state. This is what I want to do:
inputChangeHandler : function (event) {
this.setState( { event.target.id : event.target.value } );
},
where event.target.id is used as the state key to be updated. Is this not possible in React?
In loop with
.map
work like this:Note the
[]
intype
parameter. Hope this helps :)Thanks to @Cory's hint, i used this:
If using ES6 or the Babel transpiler to transform your JSX code, you can accomplish this with computed property names, too:
Can use a spread syntax, something like this:
Just wanted to add, that you can also de-structuring to refactor the code and make it look neater.
When you need to handle multiple controlled input elements, you can add a name attribute to each element and let the handler function choose what to do based on the value of event.target.name.
For example:
How I accomplished this...