React-dnd: How can I use dispatch in a connectDropTarget component?

Created on 26 Feb 2016  ·  4Comments  ·  Source: react-dnd/react-dnd

when I use react-DND , found a problem.

question, I wanna to use dispatch in a connectDropTarget component.

const dropTodoTarget = {
  drop(props,monitor,component) {
      var todo = monitor.getItem();
      if (todo.board_id != props.board_id) {
          component.localHandleMove(todo.todo_id);
      }
      return {};
  }
};


function collect(connect, monitor) {
  return {
    connectDropTarget: connect.dropTarget(),
    isOver: monitor.isOver()
  };
}
export default class TodoList extends Component {

    constructor(props) {
        super(props);
        this.localHandleMove = this.localHandleMove.bind(this);
    }

    localHandleMove(todo_id)  {
        this.props.dispatch(actions.completeTodo(todo_id,this.props.board_id))
    }

    render() {
        const { connectDropTarget } = this.props;
        //                  
        return connectDropTarget(
            <ul className="todo-list">
            {this.props.todos.map(function(todo){
               return <Todo key={todo.todo_id} todo={todo} />
            })}
            </ul>
        );
    } 
}


export default flow(
  connect(),
  DropTarget(ItemTypes.TODO, dropTodoTarget, collect)
)(TodoList);

when I wrote like this.

Uncaught TypeError: component.localHandleMove is not a function

I wanna how could I use dispatch in a component?

thanks

Most helpful comment

No, component.store is an instance variable that is a private API and not documented anywhere. It might be gone in any version of React Redux so please don’t rely on it.

What you want is to put connect() _after_ DropTarget() so that props injected by connect() (including dispatch) are available to you in the drop target:

const dropTodoTarget = {
  drop(props,monitor,component) {
      var todo = monitor.getItem();
      if (todo.board_id != props.board_id) {
          props.dispatch(actions.completeTodo(todo.board_id,this.props.board_id))
      }
      return {};
  }
};

// ...

export default flow(
  DropTarget(ItemTypes.TODO, dropTodoTarget, collect),
  connect()
)(TodoList);

All 4 comments

finally, i found could be doing like this:

const dropTodoTarget = {
      drop(props,monitor,component) {
      var todo = monitor.getItem();
      if (todo.board_id != props.board_id) {
    component.store.dispatch(actions.moveTodoBoard(todo.todo_id,todo.board_id,props.board_id));
      }
      return {};
  }
};

No, component.store is an instance variable that is a private API and not documented anywhere. It might be gone in any version of React Redux so please don’t rely on it.

What you want is to put connect() _after_ DropTarget() so that props injected by connect() (including dispatch) are available to you in the drop target:

const dropTodoTarget = {
  drop(props,monitor,component) {
      var todo = monitor.getItem();
      if (todo.board_id != props.board_id) {
          props.dispatch(actions.completeTodo(todo.board_id,this.props.board_id))
      }
      return {};
  }
};

// ...

export default flow(
  DropTarget(ItemTypes.TODO, dropTodoTarget, collect),
  connect()
)(TodoList);

What you want is to put connect() after DropTarget()...

Isn't it suppose to come before?

No, component.store is an instance variable that is a private API and not documented anywhere. It might be gone in any version of React Redux so please don’t rely on it.

What you want is to put connect() _after_ DropTarget() so that props injected by connect() (including dispatch) are available to you in the drop target:

const dropTodoTarget = {
  drop(props,monitor,component) {
      var todo = monitor.getItem();
      if (todo.board_id != props.board_id) {
          props.dispatch(actions.completeTodo(todo.board_id,this.props.board_id))
      }
      return {};
  }
};

// ...

export default flow(
  DropTarget(ItemTypes.TODO, dropTodoTarget, collect),
  connect()
)(TodoList);

What is flow here?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Okami92 picture Okami92  ·  3Comments

redochka picture redochka  ·  3Comments

greggigon picture greggigon  ·  4Comments

gocreating picture gocreating  ·  4Comments

antoineBernard picture antoineBernard  ·  3Comments