简体   繁体   English

React Redux - 尝试使用 React-DnD 和 mapDispatchToProps 时遇到问题

[英]React Redux - Trouble trying to use React-DnD and mapDispatchToProps

I was wondering why couldn't I get some of my components to work using ReactDnD and mapDispatchToProps.我想知道为什么我不能使用 ReactDnD 和 mapDispatchToProps 让我的一些组件工作。

I'm trying to drag and drop Services to Clients but I can't find my dispatch functions in props at my serviceSpec on the endDrag method.我正在尝试将服务拖放到客户端,但我无法在 endDrag 方法的 serviceSpec 的 props 中找到我的调度函数。

Considering my mapDispatchToProps on my Service component:考虑我的 Service 组件上的 mapDispatchToProps:

const mapDispatchToProps = (dispatch) => ({
  dragService: (service) => { dispatch(dragService(service)) },
  dropService: (service, clientTarget) => { dispatch(dropService(service, clientTarget)) }
});

High-order functions to bond together DragSource + Service + State + Dispatch:将 DragSource + Service + State + Dispatch 绑定在一起的高阶函数:

var reduxConnectedService = connect(mapStateToProps, mapDispatchToProps)(Service);
export default DragSource('service', serviceSpec, collect)(reduxConnectedService);

render() method:渲染()方法:

render (){
    const { isDragging, connectDragSource, service } = this.props;
    return connectDragSource(
      <a className="panel-block is-active">
        <CategoryIcon category={service.category}/>
        {service.name} | ${service.price}
      </a>
    )
  }

The spec object used to implement the dragSource specification ( here is the problem ):用于实现 dragSource 规范的规范对象(这里是问题):

const serviceSpec = {
  beginDrag(props) {
    return props.service;
  },
  endDrag(props, monitor, component){
    console.log(props);
  }
}

The console.log at endDrag function just show my Service Object because is being returned on the beginDrag function: endDrag 函数中的 console.log 只显示我的服务对象,因为它是在 beginDrag 函数上返回的:

{service: {…}}

But my plan was to dispatch the action dropService here on endDrag, but I couldn't.但我的计划是在 endDrag 上分派 dropService 动作,但我不能。 The documentation says that ( http://react-dnd.github.io/react-dnd/docs/api/drag-source ):文档说( http://react-dnd.github.io/react-dnd/docs/api/drag-source ):

beginDrag(props, monitor, component): Required. beginDrag(props, monitor, component):必需。 When the dragging starts, beginDrag is called.当拖动开始时,beginDrag 被调用。 You must return a plain JavaScript object describing the data being dragged.您必须返回一个描述被拖动数据的普通 JavaScript 对象。 What you return is the only information available to the drop targets about the drag source so it's important to pick the minimal data they need to know.您返回的是有关拖放源的放置目标可用的唯一信息,因此选择他们需要知道的最少数据很重要。 You may be tempted to put a reference to the component into it, but you should try very hard to avoid doing this because it couples the drag sources and drop targets.您可能很想将组件的引用放入其中,但您应该尽量避免这样做,因为它耦合了拖动源和放置目标。 It's a good idea to return something like { id: props.id } from this method.从这个方法返回类似 { id: props.id } 的东西是个好主意。

I don't believe that I should return the dropService(dispatch) function on the beginDrag definition.我不认为我应该在 beginDrag 定义上返回 dropService(dispatch) 函数。 So after hours trying to make it work, I started to pass the dropService function as a prop directly through the parent component (ServiceList):因此,经过几个小时的尝试,我开始将 dropService 函数作为 prop 直接通过父组件 (ServiceList) 传递:

{this.props.filteredServices.map((service, index) => (
     <Service service={service} key={service.name} dropService={this.props.dropService}/>
))}

Making this way I could dispatch the dropService action on the endDrag method like I wanted, the console.log can proves that:通过这种方式,我可以像我想要的那样在 endDrag 方法上调度 dropService 操作,console.log 可以证明:

{service: {…}, dropService: ƒ}

I could make it work but I can't understand why I couldn't get this to work using mapDispatchToProps.我可以让它工作,但我不明白为什么我不能使用 mapDispatchToProps 让它工作。 Is there any limitation while using React-DnD or am I making something wrong?使用 React-DnD 有什么限制还是我做错了什么?

Any help will be appreciated, I cannot die with this doubt.任何帮助将不胜感激,我不能因这种怀疑而死。 Thank you in advance.先感谢您。

Your problem is with these two lines:你的问题是这两行:

var reduxConnectedService = connect(mapStateToProps, mapDispatchToProps)(Service);
export default DragSource('service', serviceSpec, collect)(reduxConnectedService);

Note the order: you wrap Service into a Redux container.注意顺序:将Service包装到 Redux 容器中。 Then you wrap the Redux Container with the DragSource container.然后用DragSource容器包装 Redux 容器。 Thus, in the component tree, the drag container is the parent of the Redux container, which means it doesn't receive the Redux props from it.因此,在组件树中,拖动容器是 Redux 容器的容器,这意味着它不会从它接收 Redux 道具。

To fix that, make the drag container the child of the Redux container.为了解决这个问题,让拖动容器成为 Redux 容器的容器。 You can do so by simply swapping the DragSource() and connect() calls:您可以通过简单地交换DragSource()connect()调用来实现:

var dragService = DragSource('service', serviceSpec, collect)(Service);
var reduxConnectedService = connect(mapStateToProps, mapDispatchToProps)(dragService);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM