import { useEffect } from 'react';
import { useDrag, useDrop } from 'react-dnd';
import { getEmptyImage } from 'react-dnd-html5-backend';
import { useAppState } from '../AppStateContext';
import { DragItem } from '../DragItem';
export const useItemDrag = (item: DragItem) => {
const { dispatch } = useAppState();
const [, drag, preview] = useDrag({
item,
begin: () => dispatch({ type: 'SET_DRAGGED_ITEM', payload: item }),
end: () => dispatch({ type: 'SET_DRAGGED_ITEM', payload: undefined }),
});
useEffect(() => {
preview(getEmptyImage(), { captureDraggingState: true });
}, [preview]);
return { drag };
};
在 useDrag 上收到此错误
Argument of type '{ item: DragItem; begin: () => any; end: () => any; }' is not assignable to parameter of type 'FactoryOrInstance<DragSourceHookSpec<unknown, unknown, any>>'.
对象字面量只能指定已知属性,并且类型 'FactoryOrInstance<DragSourceHookSpec<unknown, unknown, any>>' 中不存在 'begin'