react-dnd

React dnd drag item different from actual item


I try to dnd items from a list to a bucket. The items have id, title, description

Problem:
When dragging the item labelled value: 1, title: foo , description: bar, randomly another item from the list are selected instead.

Cards List
value: 1, title: foo , description: bar
value: a, title: aaa , description: AAA
value: x, title: xxx , description: XXX
value: y, title: yyy , description: YYY
const FunctionCard = props => {

  const { value, title, description} = props

  const [{ isDragging }, drag] = useDrag(() => ({
    type: "Card",
    item: () => { console.log('item X selected', value, title, description); return ({ id:value })},
    collect: (monitor) => { 
      console.log("collector drags item X", monitor.getItem()); 
      return ({
       isDragging: monitor.isDragging(),
       handlerId: monitor.getHandlerId()
    })}
  }));
...

Solution

  • It is possible to assign dependencies to the hooks useDrag, useDrop (see github issue)

    const FunctionCard = props => {
    
      const { value, title, description} = props
    
      const [{ isDragging }, drag] = useDrag(() => ({
        type: "Card",
        item: () => { console.log('item X selected', value, title, description); return ({ id:value })},
        collect: (monitor) => { 
          console.log("collector drags item X", monitor.getItem()); 
          return ({
           isDragging: monitor.isDragging(),
           handlerId: monitor.getHandlerId()
        })}
      }), [value, title, description]);
    ...