I've recently begun learning reactjs and stumbled upon this piece of code:
handleChange = event => {
const { name, value } = event.target
this.setState({
[name]: value,
})
}
And I'm somewhat confused, in particular with the following notation:
[name]: value,
What are the [ ] used for?
I understand, that the const = { name, value } = event.target is used for object destruction into properties, but the [name] notation seems strange and I have not seen any previous reference to it in any of my javascript coding.
Despite my best efforts at googling [] javascript usage and similar, I was unable to find anything of similar sort.
My best guess would be the similarity to object["property"] expression, but this still seems a little odd compared to that.
These are called computed property names, and the notation is basically for using a variable as an object key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With