反应网格布局错误:<DraggableCore> 未安装在 DragStart 上

Jav*_*oya 5 reactjs react-grid-layout

当尝试在 ResponsiveGridLayout 中拖动任何面板或调整其大小时,出现以下错误:<DraggableCore> not mounted on DragStart!

这是我的网格布局:

<ResponsiveGridLayout
        className="layout"
        cols={{ lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 }}
        onLayoutChange={(layout, allLayouts) => handleLayoutChange(allLayouts)}
        rowHeight={30}
        layouts={layouts}
        measureBeforeMount={false}
        compactionType="vertical"
        useCSSTransforms={true}
    >
        <Panel key="a" title="Interactions per country">
            <PieGraph />
        </Panel>
    </ResponsiveGridLayout>
Run Code Online (Sandbox Code Playgroud)

这是每个单独的面板:

export const Panel: React.FC<IPanelProps> = (props) => {
const {className, children, title, shouldScroll, ...defaultPanelProps} = props;
let scrollClass = shouldScroll ? " scroll-y" : "";

return (
    <div {...defaultPanelProps} className={`custom-panel wrapper ${className}`} >
            {title && <div className="custom-panel-title text-medium">{title}</div>}

            <div className={`custom-panel-content ${scrollClass}`} onMouseDown={ e => e.stopPropagation() }>
                {children}
            </div>
    </div>
);
Run Code Online (Sandbox Code Playgroud)

};

Jav*_*oya 4

我通过向自定义组件添加“ref”来修复此问题<Panel/>。仅当您的react-grid-layout 中有自己的组件(而不是带键的div)时,才会出现此错误。

要创建引用,只需将const ref = React.createRef()其传递给您的自定义面板组件,如下所示:

<ResponsiveGridLayout
        className="layout"
        cols={{ lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 }}
        onLayoutChange={(layout, allLayouts) => handleLayoutChange(allLayouts)}
        rowHeight={30}
        layouts={layouts}
        measureBeforeMount={false}
        compactionType="vertical"
        useCSSTransforms={true}
    >
        <Panel ref={ref} key="a" title="Liters per active country">
            <PieGraph />
        </Panel>
    </ResponsiveGridLayout>
Run Code Online (Sandbox Code Playgroud)

您的自定义面板将变为:

export const Panel = React.forwardRef((props: IPanelProps, ref) => {
const { className, children, title, shouldScroll, ...defaultPanelProps } = props as any;
let scrollClass = shouldScroll ? " scroll-y" : "";

return (
    <div ref={ref} {...defaultPanelProps} className={`custom-panel wrapper ${className}`} >

        {title && <div className="custom-panel-title text-medium">{title}</div>}

        <div className={`custom-panel-content ${scrollClass}`} onMouseDown={e => e.stopPropagation()}>
            {children}
        </div>
    </div>
);
Run Code Online (Sandbox Code Playgroud)

});

请注意React.forwardRef((props: IPanelProps, ref)和 的支柱ref={ref}