【发布时间】:2020-10-01 16:49:46
【问题描述】:
当我以自己的方式拖动项目时,我正在尝试定义项目类型。但是如果我这样做并拖动项目,就会出现问题,它会在删除项目后显示“不变违规:必须定义项目类型”。这意味着我有一个包含 ItemTypes 的文件,我在其中定义了所有内容:
export const ItemTypes = {
'CARD': "card",
'PHONE': "phone",
'WEAPON': "weapon",
'FOOD': "food",
'DRINK': "drink"
};
在主库存中,项目数组是这样的
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
}
]);
以及我定义正在拖动的项目的项目组件:
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()
})
});
所以最后这意味着当我将项目放在目标容器上时,我收到错误“不变违规:必须定义项目类型”。
【问题讨论】:
-
当
item对象中的type属性意外未定义时,我遇到了类似的问题。你的 props 对象设置正确吗?
标签: javascript reactjs react-dnd