javascriptreactjsreact-dnd

Problems with Item Types in react dnd (Invariant Violation: item type must be defined)


i'm trying to define the Item Type when i drag a item on my own way. But somehting goes wrong if i do it that way and drag the item, it shows me "Invariant Violation: item type must be defined" after droping the item. That means i have a file with ItemTypes where i define everything:

export const ItemTypes = {
    'CARD': "card",
    'PHONE': "phone",
    'WEAPON': "weapon",
    'FOOD': "food",
    'DRINK': "drink"
};

and on the main inventory the item array's like that

  const [items, setItems] = useState([
    {
      type: "PHONE",
      label: "test",
      itemname: "test",
      pieces: 10,
      itemweight: 0.2
    },
    {
      type: "CARD",
      label: "test5",
      itemname: "test5",
      pieces: 5,
      itemweight: 0.2
    }
  ]);

and the Item Component where i define the Item that is beeing dragged:

const Item = props => {

    const [{ isDragging }, drag] = useDrag({
      item: {
        type: ItemTypes[props.type],
        index: props.index,
        label: props.label,
        itemname: props.name,
        pieces: props.pieces,
        weight: props.itemweight
      },
      collect: (monitor) => ({
        isDragging: !!monitor.isDragging()
      })
    });

so at the end that means i get the error 'Invariant Violation: item type must be defined' when i drop the item on the target container.


Solution

  • There was a recent change to useDrag, see: https://github.com/react-dnd/react-dnd/releases/tag/v14.0.0