2016-11-28 38 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)'? –