Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use dispatch in a connectDropTarget component? #393

Closed
dreamcog opened this issue Feb 26, 2016 · 4 comments
Closed

How can I use dispatch in a connectDropTarget component? #393

dreamcog opened this issue Feb 26, 2016 · 4 comments

Comments

@dreamcog
Copy link

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

@dreamcog
Copy link
Author

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 {};
  }
};

@gaearon
Copy link
Member

gaearon commented Feb 28, 2016

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);

@gaearon gaearon closed this as completed Feb 28, 2016
@shem86
Copy link

shem86 commented Mar 3, 2016

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

Isn't it suppose to come before?

Titogelo added a commit to Chroma-Kids/Frontend-Service that referenced this issue Mar 20, 2018
Issue solved here. Trying to dispatch function from drop. DropTarget
should go before connect
react-dnd/react-dnd#393
@gl2748
Copy link

gl2748 commented Apr 19, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants