当尝试拖动ResponsiveGridLayout中的任何面板或调整其大小时,我收到以下错误:<DraggableCore> not mounted on DragStart!
这是我的GridLayout:
<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>
);};
发布于 2021-04-12 13:47:46
我通过向我的自定义<Panel/>组件添加一个"ref“修复了这个问题。这个错误似乎只有在您的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>您的自定义面板将变为:
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)和属性。
https://stackoverflow.com/questions/67053157
复制相似问题