当我以自己的方式拖动项目时,我试图定义项目类型。但是,如果我这样做并拖动项目,就会出现问题,它会在删除项目后向我显示“不变违规:必须定义项目类型”。这意味着我有一个包含 ItemTypes 的文件,其中定义了所有内容:
export const ItemTypes = {
'CARD': "card",
'PHONE': "phone",
'WEAPON': "weapon",
'FOOD': "food",
'DRINK': "drink"
};
Run Code Online (Sandbox Code Playgroud)
在主库存上,项目数组就像这样
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
}
]);
Run Code Online (Sandbox Code Playgroud)
以及项目组件,我在其中定义被拖动的项目:
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: …Run Code Online (Sandbox Code Playgroud)