如何在 ES6 中的两个扩展运算符之间设置逻辑运算符

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

我正在做一个 React 项目,我想在这样的组件中初始化状态:

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

其中 initValues 是一个包含所有道具但具有空值的对象 我知道这在语法上是不正确的,我只是想要一个替代方案

提前致谢

您可以先采用初始值进行传播,然后采用所需的属性。

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

你可以传播表达的结果。如果您只想要道具或初始值。

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