This question already has answers here:
What does curly brackets in the `var { … } = …` statements do?

(4个答案)


4年前关闭。



render () {
  const { params } = this.props.params
  return (
   <div>{ params.article }</div>
  )
}

为什么参数需要用大括号括起来才可能是“const params = this.props.params”

最佳答案

这称为destructuring
而不是做

const params = this.props.params.params
你可以用短手
const { params } = this.props.params.
看例子

const a = {
  key: 'value',
  key2: 'value2',
}

const {key} = a //getting a.key from a and assigning it to key

console.log(key)

const { key2: newVar } = a //getting a.key2 from a and assigning it to newVar

console.log(newVar)

希望这可以帮助!

关于javascript - 为什么const名称需要在ReactJS中用大括号括起来,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40215662/

10-12 13:33