314

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?

Community
  • 1
  • 1
trad
  • 7,581
  • 4
  • 20
  • 17
  • 4
    This is a duplicate of any question concerning dynamic object keys. It's not specific to react – Cory Danielson Mar 26 '15 at 14:06
  • 11
    var newstate = {}; newstate[event.target.id] = event.target.id; this.setState(newstate); – Cory Danielson Mar 26 '15 at 14:08
  • Thank you, I didn't have a good handle on using objects in general. – trad Mar 26 '15 at 14:45
  • 1
    Possible duplicate of [How do I create a dynamic key to be added to a JavaScript object variable](http://stackoverflow.com/questions/2462800/how-do-i-create-a-dynamic-key-to-be-added-to-a-javascript-object-variable) – ivarni Apr 22 '16 at 11:04
  • @trad I'm with this problem but, what did you put on your initial State? It doesn't matter, right? – Raphael Onofre Sep 29 '16 at 18:55

14 Answers14

321

Thanks to @Cory's hint, i used this:

inputChangeHandler : function (event) {
    var stateObject = function() {
      returnObj = {};
      returnObj[this.target.id] = this.target.value;
         return returnObj;
    }.bind(event)();

    this.setState( stateObject );    
},

If using ES6 or the Babel transpiler to transform your JSX code, you can accomplish this with computed property names, too:

inputChangeHandler : function (event) {
    this.setState({ [event.target.id]: event.target.value });
    // alternatively using template strings for strings
    // this.setState({ [`key${event.target.id}`]: event.target.value });
}
trad
  • 7,581
  • 4
  • 20
  • 17
182

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:

inputChangeHandler(event) {
  this.setState({ [event.target.name]: event.target.value });
}
vikram jeet singh
  • 3,416
  • 2
  • 21
  • 22
  • 9
    what do the brackets around [event.target.name] indicate? Why are they required? – user798719 Feb 23 '18 at 06:50
  • 1
    As compared to usual approach to named each element separately like this.setState({ userName: e.target.value }); This will handle multiple elements of form as an array and no need to set each individual element – vikram jeet singh Feb 23 '18 at 09:27
  • 1
    but still how do I access that state in same way ? like `this.state([event.target.name])` ? – Kirankumar Dafda Aug 30 '18 at 09:48
  • 1
    I think [MDN web docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) and [this post](https://medium.com/@bretdoucette/understanding-this-setstate-name-value-a5ef7b4ea2b4) explains why we need the brackets. – Skelli Feb 01 '20 at 10:08
50

How I accomplished this...

inputChangeHandler: function(event) {
  var key = event.target.id
  var val = event.target.value
  var obj  = {}
  obj[key] = val
  this.setState(obj)
},
sintaxi
  • 1,858
  • 14
  • 10
  • I did similar way but the problem was it still did not render the component, and I ran pillar to post (including this :D), and somewhere found this: `this.forceUpdate();`which should not have been the case with latest React. Let's see whats the issue later!! – xploreraj Feb 18 '18 at 13:16
31

I just wanted to add, that you can also use de-structuring to refactor the code and make it look neater.

inputChangeHandler: function ({ target: { id, value }) {
    this.setState({ [id]: value });
},
connect2Coder
  • 1,122
  • 1
  • 13
  • 25
saulable
  • 475
  • 5
  • 14
15
this.setState({ [`${event.target.id}`]: event.target.value}, () => {
      console.log("State updated: ", JSON.stringify(this.state[event.target.id]));
    });

Please mind the quote character.

javatar
  • 4,542
  • 14
  • 50
  • 67
12

I had a similar problem.

I wanted to set the state of where the 2nd level key was stored in a variable.

e.g. this.setState({permissions[perm.code]: e.target.checked})

However this isn't valid syntax.

I used the following code to achieve this:

this.setState({
  permissions: {
    ...this.state.permissions,
    [perm.code]: e.target.checked
  }
});
Matthew Holtom
  • 121
  • 1
  • 3
11

In loop with .map work like this:

{
    dataForm.map(({ id, placeholder, type }) => {
        return <Input
            value={this.state.type}
            onChangeText={(text) => this.setState({ [type]: text })}
            placeholder={placeholder}
            key={id} />
    })
}

Note the [] in type parameter. Hope this helps :)

yanckst
  • 438
  • 4
  • 17
Johnatan Maciel
  • 553
  • 6
  • 10
8

I was looking for a pretty and simple solution and I found this:

this.setState({ [`image${i}`]: image })

Hope this helps

Catalina
  • 97
  • 1
  • 3
4

With ES6+ you can just do [${variable}]

Jujhar Singh
  • 129
  • 6
2

when the given element is a object:

handleNewObj = e => {
        const data = e[Object.keys(e)[0]];
        this.setState({
            anykeyofyourstate: {
                ...this.state.anykeyofyourstate,
                [Object.keys(e)[0]]: data
            }
        });
    };

hope it helps someone

1

Your state with dictionary update some key without losing other value

state = 
{   
    name:"mjpatel"  
    parsedFilter:
    {
      page:2,
      perPage:4,
      totalPages: 50,
    }
}

Solution is below

 let { parsedFilter } = this.state
 this.setState({
      parsedFilter: {
        ...this.state.parsedFilter,
        page: 5
      }
  });

here update value for key "page" with value 5

manoj patel
  • 1,150
  • 12
  • 10
1

If you are using JS ES6+, it is [${variable}], to set the key:

// In React
onChange={e => setData({ [variable] : e.target.value })}

As well, if you want to access the dynamic key, you just use data[${variable}]:

// In React
value={data[variable]}
Rokas Rudzianskas
  • 582
  • 1
  • 7
  • 10
0

try this one please.

State is here as example

 this.state = {
            name: '',
            surname: '',
            username: '',
            email: '',
  }

Onchange function is here.

onChangeData = (type, event) => {
    const stateData = this.state;
    if (event === "" || event === "Seçiniz")
        stateData[type] = undefined
    else
        stateData[type] = event

    this.setState({ stateData});
}
-3

Can use a spread syntax, something like this:

inputChangeHandler : function (event) {
    this.setState( { 
        ...this.state,
        [event.target.id]: event.target.value
    } );
},
sta
  • 17
  • 7
    React will do the object merging for you, this is bad practice. – tim-phillips Mar 14 '17 at 19:50
  • 1
    basically if you have some inner object and you want to change one propery on that inner object you do it like this: this.setState({selectedItems:{...selectedItems,[item.id]:true}}) – Eran Or Jan 28 '18 at 15:56
  • @Rohmer : Why is this considered a bad practice ? If an object has multiple keys, using the spread operator seems to work to preserve all other keys except the one being modified. Or should we use prevState ? Please explain. – Sam Oct 03 '20 at 21:33
  • If you have nested state like @EranOr mentioned, then doing spread makes sense, but you must use `prevState` or you won’t be able to guarantee that you’re using the most up-to-date state. However, if you’re just setting a single, top-level state value like in @sta’s answer, then you don’t need to spread on state since React will merge it in for you. https://reactjs.org/docs/state-and-lifecycle.html#state-updates-are-merged – tim-phillips Oct 04 '20 at 22:14