簡體   English   中英

如何在 ES6 中的兩個擴展運算符之間設置邏輯運算符

[英]How can I set a logical operator between two spread operators in ES6

我正在做一個反應項目,我想在這樣的組件中初始化 state:

state = {
        ...this.props || ...this.initValues
    }

其中 initValues 是一個 object 包含所有道具但具有空值我知道這在語法上不正確,我只是想要一個替代

提前致謝

您可以先獲取初始值進行傳播,然后再獲取所需的屬性。

state = {
    ...this.initValues,
    ...this.props
};

您可以傳播表達式的結果。 如果您只想要道具或初始值。

state = {
    ...(Object.keys(this.props).length === 0 ? this.initValues : props)
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM