2

当试图在我的 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>

这是每个单独的面板:

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>
);

};

4

1 回答 1

2

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

要创建 ref,只需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>

您的自定义面板变为:

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>
);

});

注意React.forwardRef((props: IPanelProps, ref)和 的道具ref={ref}

于 2021-04-12T05:47:45.793 回答