简体   繁体   English

使用条件赋值时丑陋的解构

[英]Ugly destructuring when using conditional assignment

ESLint forcing to use object destructuring when working with object properties and in some cases it leads to redundant lines of code. 在处理对象属性时,ESLint强制使用对象解构,在某些情况下,它会导致冗余的代码行。

According to ESLint, yo can't do something like (which feels like the right way to do so): 根据ESLint,哟不能做类似的事情(感觉这是正确的方式):

const { value } = props;
const color = props.color || '#515cdc';

Instead, it forces you to do it like so: 相反,它会强迫你这样做:

const { value } = props;
let { color } = props;
color = color || '#515cdc';   

Am i missing something or is there any other way to do it? 我错过了什么或者还有其他办法吗?

Use a default value while destructuring: 在解构时使用默认值

 const props = { value: 10 }; const { value, color = '#515cdc' } = props; console.log(value, color); 

Note: you can also turn off the annoying rule. 注意:您也可以关闭恼人的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM