When I used to setState
the value for the User Name statically, I used to use the below code to set the value in the variable name.
this.setState({ 'userName' : event.target.value })
but the same when I used to set the same value in the same key name but this time dynamically.
In my HTML attribute I set the name as userName
and onChange
I call the function with the event. In that event.target.name
I get the userName
and event.target.value
I get the value that I typed but during the setState
, the below code did not work:
this.setState({ event.target.name : event.target.value })
Instead of that, I used to set the key by using [event.targe.name]
that's working fine:
this.setState({ [event.target.name] : event.target.value })
Actually what I need is, what is the difference between the above two codes and how does it work in statically and dynamically?