2015-11-26 21 views
13

Ben an example of the React-dnd project okuyorum:

moveCard(dragIndex, hoverIndex) { 
    const { cards } = this.state; 
    const dragCard = cards[dragIndex]; 

    this.setState(update(this.state, { 
     cards: { 
     $splice: [ 
      [dragIndex, 1], 
      [hoverIndex, 0, dragCard] 
     ] 
     } 
    }));} 

mı aynı bir on this page açıkladı bu $ ekleme?

Bu kod parçası ne yapar? $splice işlevi benim için çok karışık.

cevap

16

Böyle

newcards.splice(dragIndex, 1); // removing what you are dragging. 
newcards.splice(hoverIndex, 0, dragCard); // inserting it into hoverIndex. 

yerine doğrudan hedef dizi manipüle bu Değiştirilemezlik Yardımcıları oluşturup yeni bir devlet atayarak durumunu güncellemesine yardımcı olarak düz ekleme fonksiyonları ile ilgili sabit bir versiyonu temelde bu.