简体   繁体   中英

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.

According to ESLint, yo can't do something like (which feels like the right way to do so):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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