【问题标题】:Negate A Destructured Value Assigned To A New Variable否定分配给新变量的解构值
【发布时间】:2019-01-17 20:55:14
【问题描述】:

我在 React 组件中有以下代码。

const { isDisclosed: showDisclosureButton } = this.state

问题是我需要showDisclosureButton 的值是this.state.isDisclosed 的否定。相当于:

const showDisclosureButton = !this.state.isDisclosed

有没有办法否定分配给新变量的解构值?

【问题讨论】:

  • 我觉得没办法
  • 为什么不使用:const { isDisclosed } = this.state; const showDisclosureButton = !isDisclosed;
  • @Troopers 谢谢。是的,我认为这是最好的选择。
  • @Troopers 为什么要使用解构?但如果你是粉丝:const {state: {isDisclosed}} = this;:P

标签: javascript ecmascript-6 destructuring


【解决方案1】:

不,没有。随便写

const showDisclosureButton = !this.state.isDisclosed

它比任何解构都简单、清晰且简短。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 2014-05-22
    • 2019-06-25
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多