2017-07-07 92 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 
    } 
}