2017-07-07 45 views
0

我有一個組件可以維護一個狀態中的對象列表。我打算讓這個組件成爲一個Drag Source。當Drag事件開始時,我希望拖動組件狀態中的對象列表。 問題是Drag Source Spec對象的方法beginDrag將組件的prop作爲參數。這意味着可以拖動的對象必須從道具派生。如何訪問DragSource規範中的組件狀態

draggedItemSpec { 
     beginDrag(props, monitor, connect) 
     { 
     /*how do I access component state here? 
Why there is a limitation that when creating dragged object, you should only use props?*/ 
     } 
    } 

回答

0

的第三個參數beginDrag實際上是「組件」(每個:http://react-dnd.github.io/react-dnd/docs-drag-source.html),它提供了訪問到在向下降,所以你beginDrag中,你實際上可以做

的陣營組件的實例
draggedItemSpec { 
    beginDrag(props, monitor, component) 
    { 
     const currentState = component.getState(); 
     // Do things with that state 
    } 
}