2016-11-28 35 views
0

下面是我的代碼段通行證參考發生反應+ REDUX

const slipSource = { 
    endDrag(props, monitor) { 
     const item = monitor.getItem(); 
     const dropResult = monitor.getDropResult(); 
     this.props.updateSelections(selections) 
    } 
}; 
const mapDispatchToProps = (dispatch) => { 
     return { 
      updateSelections: (selections) => { 
      dispatch(updateSelections(selections)) } 
     } 
} 

在這裏,n這個代碼我打電話updateSelections()slipSource OBJ內部方法。 但我的方法沒有在obj中獲取「this」的引用,導致endDrag()函數在obj上下文中。

如何在endDrag()函數中獲取「this」的引用。

+0

endDrag收到另一個ARG'endDrag(道具,顯示器,元器件)',您可以使用組件,因爲這,不知道component.props將工作,文檔:http://gaearon.github.io/react-dnd/docs-drag-source.html –

+0

@YanMayatskiy 我稱之爲component.dispatchProps.updateSelections(選擇) 是否正確。 – Supriya

+0

@YanMayatskiy,是的,component.props會按預期工作。 –

回答

0
endDrag(props, monitor, component){} 

component參數表現爲this,所以component.props應該工作一樣this.props

從文檔:http://gaearon.github.io/react-dnd/docs-drag-source.html

例如代碼組件參數:https://github.com/gaearon/react-dnd/blob/master/examples/03%20Nesting/Drop%20Targets/Dustbin.js

drop(props, monitor, component) { 
    ... 
    component.setState({ 
     ... 
    }); 
    } 
+0

我在調用endDrag()函數內的component.props.updateSelections(選項) 。獲取以下錯誤 main-2dd52dc ... .js:38未捕獲的ReferenceError:updateSelections未定義(...) – Supriya

+0

您不能只從連接訪問此功能?或其他道具? –

+0

您是否嘗試過使用道具參數'props.updateSelections(selections)'? –