传播不适用于null

时间:2017-06-23 06:17:05

标签: javascript ecmascript-6

ES6传播不会以这种方式工作

I have this.state = { sortedKey: null } in my constructor and I do

this.setState({
sortedKey: [...this.state.sortedKey, 'something']
})

我收到Cannot convert undefined or null to object的错误?

1 个答案:

答案 0 :(得分:1)

var state = { "sortedKey": [] };

var newState = {"sortedKey": [...state.sortedKey, 'something']};

console.log(newState)