2017-08-01 33 views
0

任何人曾與React拖動&拖放,需要幫助!React拖放當前組件上下文

在此示例中(https://github.com/react-dnd/react-dnd/blob/master/examples/01%20Dustbin/Multiple%20Targets/Dustbin.js#L20)在第20行,有props.onDrop(monitor.getItem());這個代碼,其中通過props傳遞到部件Dustbin功能onDrop被調用。

我需要知道是否有方法調用Dustbin中定義的方法,而不是通過道具傳遞。

如:this.onDrop(monitor.getItem());currentComponent.onDrop(monitor.getItem());

回答

0

是有,第三個參數下降是「組件」(在這裏看到:http://react-dnd.github.io/react-dnd/docs-drop-target.html),這是被投進的實際組件。所以,你可以這樣做:

const dustbinTarget = { 
    drop(props, monitor, component) { 
    component.onDrop(monitor.getItem()); 
    }, 
}; 

這種說法是適用於所有的除外canDrop放置目標的方法,因爲實例可能不可用在它被調用的時間。