简体   繁体   English

跳过为const赋值

[英]skip assigning value to const

I'm using flux and I found below code is not elegant. 我使用的是助焊剂,发现下面的代码并不优雅。 Can't I skip the const here? 我不能在这里跳过const吗? Is there any way to make it one line? 有什么办法使它成为一行吗?

static getPropsFromStores(props) {
    const { items } = CrudStore.getState();
    return { items };
}

The existing line: 现有行:

const { items } = CrudStore.getState();

...is a destructuring assignment equivalent to doing this: ...是等同于执行以下操作的解构任务

const items = CrudStore.getState().items;

Whereas this line: 而此行:

return { items };

...uses a shorthand object literal equivalent to this: ...使用与此等效的速记对象文字

return { items: items };

That is, it creates and returns a new object with a property called items with a value set to whatever the existing items variable is. 也就是说,它创建并返回一个新的对象,该对象的属性称为items ,其值设置为现有items变量的值。

So a one-line way of returning an object in the format {items: items} without creating the const would be: 因此,不创建const可以{items: items}格式返回对象的单行方法是:

static getPropsFromStores(props) {
    return { items: CrudStore.getState().items };
}

Write it like this: 像这样写:

static getPropsFromStores(props) {
    return {items: CrudStore.getState().items || null};
}

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

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