我正在使用 react-beautiful-dnd 处理拖放场景,尝试按照手册https://github.com/atlassian/react-beautiful-dnd/blob/master/docs/guides/添加拖放动画拖放动画.md。
isDragging
当我将属性添加到一个简单的 div 元素时,使用 typescript 我遇到了麻烦。试图解决这个问题,我正在构建一个具有扩展属性的组件,如下
在 Typescript React App 中指定特定的道具并接受一般的 HTML 道具
这在某种程度上有效,但在 ref 属性上失败了。
我的代码:
interface IdragDiv extends React.HTMLAttributes<HTMLDivElement>, React.RefAttributes<HTMLDivElement> {
isDragging: boolean;
}
export class DragDiv extends React.Component<IdragDiv> {
render() {
const { children, ...rest } = this.props;
return <div {...rest}>{children}</div>;
}
}
像这样称呼它:
...
return (
<Draggable
draggableId={...}
>
{(provided, snapshot) => {
return (
<DragDiv
{...className}
ref={provided.innerRef}
{...provided!.draggableProps}
{...provided!.dragHandleProps}
isDragging={snapshot.isDragging && !snapshot.isDropAnimating}
>
...
</DragDiv>
);
}}
</Draggable>
);
...
这会导致以下错误:
(property) ref?: (string & ((instance: HTMLDivElement | null) => void)) | (string & React.RefObject<HTMLDivElement>) | (((instance: DragDiv | null) => void) & ((instance: HTMLDivElement | null) => void)) | ... 4 more ... | undefined
No overload matches this call.
Overload 1 of 2, '(props: Readonly<IdragDiv>): DragDiv', gave the following error.
Type '(element?: HTMLElement | null | undefined) => any' is not assignable to type '(string & ((instance: HTMLDivElement | null) => void)) | (string & RefObject<HTMLDivElement>) | (((instance: DragDiv | null) => void) & ((instance: HTMLDivElement | null) => void)) | ... 4 more ... | undefined'.
Type '(element?: HTMLElement | null | undefined) => any' is not assignable to type 'string & ((instance: HTMLDivElement | null) => void)'.
Type '(element?: HTMLElement | null | undefined) => any' is not assignable to type 'string'.
Overload 2 of 2, '(props: IdragDiv, context?: any): DragDiv', gave the following error.
Type '(element?: HTMLElement | null | undefined) => any' is not assignable to type '(string & ((instance: HTMLDivElement | null) => void)) | (string & RefObject<HTMLDivElement>) | (((instance: DragDiv | null) => void) & ((instance: HTMLDivElement | null) => void)) | ... 4 more ... | undefined'.
Type '(element?: HTMLElement | null | undefined) => any' is not assignable to type 'string & ((instance: HTMLDivElement | null) => void)'.
Type '(element?: HTMLElement | null | undefined) => any' is not assignable to type 'string'.
任何想法,有没有另一种方法来解决这个问题?