首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-grid-layout错误: DragStart上未装载<DraggableCore>

react-grid-layout错误: DragStart上未装载<DraggableCore>
EN

Stack Overflow用户
提问于 2021-04-12 13:40:37
回答 1查看 831关注 0票数 2

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

这是我的GridLayout:

代码语言:javascript
复制
<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>

下面是每个单独的面板:

代码语言:javascript
复制
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>
);

};

EN

回答 1

Stack Overflow用户

发布于 2021-04-12 13:47:46

我通过向我的自定义<Panel/>组件添加一个"ref“修复了这个问题。这个错误似乎只有在您的react-grid-layout中有自己的组件(而不是带有键的div )时才会发生。

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

代码语言:javascript
复制
<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>

您的自定义面板将变为:

代码语言:javascript
复制
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>
);

});

注意ref={ref}React.forwardRef((props: IPanelProps, ref)和属性。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67053157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档