undefinedfix
Sign in

this.setState In this case, the value of key is wrapped with []. What is the syntax?

AnirbanDebnath edited in Fri, 16 Sep 2022

What do you mean when you see this grammar on react's official website?

clipboard.png

Code source : https :// reactjs . org / docs / form ...

this.setState({[name]:value})//这是什么语法?name外面为何要用[]包裹
5 Replies
user121906
commented on Sat, 17 Sep 2022

That's how it's understood. If the name here does not use the package, does it mean that the key here has been specified, and the name is called name. But if you wrap it with [] it means that name can be a variable.

fjadskl
commented on Sat, 17 Sep 2022

http://es6.ruanyifeng.com/#do...

talatccan
commented on Sat, 17 Sep 2022

Here, the name is wrapped in brackets, which means that the value of the name variable is used as the key

yangmole
commented on Sat, 17 Sep 2022

[ name ] Is the dynamic creation of object properties, similar to obj [ name ] Like that.

daydreamer
commented on Sat, 17 Sep 2022

Represents a key that evaluates an expression as an object. You can imagine that there is a variable name ='Hello '. If you want to use' hello 'as a key, how do you write it? The following writing has the same effect:

let data = {
  [name]: '123';
}

let data = {
  hello: '123';
}