【问题标题】:react-dnd getDecoratedComponentInstance() is not a functionreact-dnd getDecoratedComponentInstance() 不是函数
【发布时间】:2023-03-21 00:05:01
【问题描述】:

我目前正在为 react 中的文件上传和排序构建一个功能。

我使用了以下示例:

一切正常,直到 eslint 告诉我不要在下面我的存储库中的 js/componenets/File.jsx 中使用 findDOMNode。

https://github.com/GregHolmes/react-dnd-dropzone

当我尝试重新排序图像的位置时会发生这种情况。即将第二张图片拖到第一名。

经过搜索,我找到了一个有关如何解决此问题的示例。然而,这个例子是行不通的。这个例子是:React DnD: Avoid using findDOMNode

与他们的示例一样,我尝试了以下方法:

js/components/File.jsx:35

<div ref={node => this.node = node} style={{ ...style, opacity }}>

然后在同一个文件中我取消注释第 62 行:

const rawComponent = component.getDecoratedComponentInstance();

并替换(第 71 行):

const hoverBoundingRect = findDOMNode(component).getBoundingClientRect();

与(第 70 行):

const hoverBoundingRect = rawComponent.node.getBoundingClientRect();

然后我得到:

getDecoratedComponentInstance() is not a function

有人知道我该如何解决这个问题吗?我为我的代码中的混乱道歉。我是新来的反应,并一直试图保持尽可能干净。

编辑

我以为我已经解决了以下问题。但是这样做意味着我无法将图像拖到另一个框。使用 DropTarget 切换 let exportFile = DragSource..... 给了我最初的问题,即函数调用不是函数。

在我的 File.jsx 文件的底部。我有:

export default flow(
DropTarget("FILE", fileTarget, connect => ({
    connectDropTarget: connect.dropTarget()
})),
DragSource("FILE", fileSource, (connect, monitor) => ({
    connectDragSource: connect.dragSource(),
    isDragging: monitor.isDragging()
}))
)(File);

我将其替换为:

function collectDragSource(connect, monitor) {
    return {
        connectDragSource: connect.dragSource(),
        isDragging: monitor.isDragging()
    };
}

function collectDropTarget(connect) {
    return {
        connectDropTarget: connect.dropTarget()
    };
}

let exportFile = DragSource('file', fileSource, collectDragSource)(File);
exportFile = DropTarget('file', fileTarget, collectDropTarget)(exportFile);

export default exportFile;

【问题讨论】:

    标签: reactjs react-dom react-dnd


    【解决方案1】:

    您实际上不需要创建rawComponent 变量并调用getDecoratedComponentInstance,它无论如何都不作为组件上的方法存在。

    node 可以通过node 属性在component 实例上简单地访问,这意味着您可以简单地做

    const hoverBoundingRect = component.node.getBoundingClientRect();
    

    顺便说一句,您的 package.json 文件中似乎也有依赖项 lodashmicroevent 重复。

    【讨论】:

    猜你喜欢
    • 2023-02-16
    • 1970-01-01
    • 2022-06-12
    • 2018-07-26
    • 2016-09-02
    • 2018-12-23
    • 2023-03-27
    • 2020-09-11
    • 2021-07-20
    相关资源
    最近更新 更多