2017-06-23 32 views
-1

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錯誤?

+0

價差僅適用於對象..如果你默認爲null,它會導致一個錯誤。將它改爲'{sortedKey:{}}' – Jalil

+2

你的問題是什麼?根據規範,拋出錯誤是預期的行爲:https://www.ecma-international.org/ecma-262/7.0/#sec-getiterator。 –

+0

@Jalil:那也會拋出一個錯誤。對象不可迭代。 –

回答

1

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

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

 
console.log(newState)