Using immutability-helper in React to set variable object key Using immutability-helper in React to set variable object key json json

Using immutability-helper in React to set variable object key


Figured it out! I needed to use ES6 computed property names and simply edit assignAttribute to:

assignAttribute(field, attribute) {  var temp = update(this.state.fields, {    [field]: {$set: attribute}  });  this.setState({    fields: temp  });}


You can use the [] syntax if you have dynamic field names:

var data = {}data[field] = {$set: attribute}var temp = update(this.state.fields, data)

This gets a lot more concise if you can use ES6.