Javascript 对分配给新变量的非结构化值求反
我在React组件中有以下代码Javascript 对分配给新变量的非结构化值求反,javascript,ecmascript-6,destructuring,Javascript,Ecmascript 6,Destructuring,我在React组件中有以下代码 const { isDisclosed: showDisclosureButton } = this.state 问题是我需要ShowDiscloseButton的值来否定this.state.isDisclosed。相当于: const showDisclosureButton = !this.state.isDisclosed 有什么方法可以对分配给新变量的非结构化值求反吗?没有。只要写 const showDisclosureButton = !this
const { isDisclosed: showDisclosureButton } = this.state
问题是我需要ShowDiscloseButton
的值来否定this.state.isDisclosed
。相当于:
const showDisclosureButton = !this.state.isDisclosed
有什么方法可以对分配给新变量的非结构化值求反吗?没有。只要写
const showDisclosureButton = !this.state.isDisclosed
它比任何解构都简单、清晰和简短。我认为没有办法不使用:
const{isDisclosed}=this.state;const showDisclosureButton=!isDisclosed代码>@士兵谢谢。是的,我认为这是最好的选择。相关:@士兵为什么要使用解构?但是如果你是一个粉丝:const{state:{isDisclosed}}=this代码>:P