【发布时间】: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