Replace array entry with spread syntax in one line of code?

前端 未结 6 782
情深已故
情深已故 2021-02-04 00:02

I\'m replacing an item in a react state array by using the ... spread syntax. This works:

let newImages = [...this.state.images]
newImages[4] = updatedImage
this         


        
6条回答
  •  粉色の甜心
    2021-02-04 00:44

    use Array.slice

    this.setState({images: [...this.state.images.slice(0, 3), updatedImage, ...this.state.images.slice(4)]})
    

提交回复
热议问题