许多人推崇不可变性,因为他们将redux与react结合使用,但我仍然看到有人使用push而不是concat。
以这段代码为例:
在这种情况下,有什么关系吗?"push above"有什么问题吗?
以这段代码为例:
submitComment() {
console.log('submitComment: '+JSON.stringify(this.state.comment))
APIManager.post('/api/comment', this.state.comment, (err, response) => {
if (err){
alert(err)
return
}
console.log(JSON.stringify(response))
let updateList = Object.assign([], this.state.list)
updatedList.push(response.result)
this.setState({
list: updatedList
})
})
}
在这种情况下,有什么关系吗?"push above"有什么问题吗?
shouldComponentUpdate
时,你会明白为什么使用不可变数据结构会更好。当然,我并不是说不可变数据结构本身就更好,只是它们通常更容易集成到shouldComponentUpdate
中。 - John Weisz