Ad

How To Fix 'Could Not Find The Drag And Drop Manager In The Context Of DragSource(Component)'

- 1 answer

I have following code

const VisiblePropsPanelItem = React.forwardRef(
({ classes, onChangeValue, attrib, isDragging, connectDragSource, connectDropTarget }, ref) => {
    const elementRef = useRef(null)
    connectDragSource(elementRef)
    connectDropTarget(elementRef)


    const opacity = isDragging ? 0 : 1
    useImperativeHandle(ref, () => ({
        getNode: () => elementRef.current,
    }))
const value = !!(attrib.value)

    return (
  <div ref={elementRef} key={ attrib.name } className = { classes.statusTaskUnit }>
        <CheckBox
          className = { classes.statusCheckBox }
          color = "primary"
          value = { value }
          onChangeValue = { () => { onChangeValue(attrib) } }
        />
        <div className = { classes.statusTextUnit }>{ attrib.name }</div>
          <IconButton aria-label="Comments">
            <DragIcon />
          </IconButton>
    </div>
    )
},
)

export default DropTarget(
CARD,
{
    hover(
  props: MyProps,
        monitor: DropTargetMonitor,
        component: CardInstance,
    ) {
        if (!component) {
            return null
        }
        const node = component.getNode()
        if (!node) {
            return null
        }
        const dragIndex = monitor.getItem().index
        const hoverIndex = props.index

        if (dragIndex === hoverIndex) {
            return
        }

        const hoverBoundingRect = node.getBoundingClientRect()

        const hoverMiddleY =
            (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2

        const clientOffset = monitor.getClientOffset()

        const hoverClientY = (clientOffset).y - hoverBoundingRect.top

        if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
            return
        }

        if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
            return
        }

    },
},
(connect: DropTargetConnector) => ({
    connectDropTarget: connect.dropTarget(),
}),
)(
DragSource(
    CARD,
    {
        beginDrag: (props: MyProps) => ({
            name: props.attrib.name,
            value: props.attrib.value,
        }),
    },
    (connect: DragSourceConnector, monitor: DragSourceMonitor) => ({
        connectDragSource: connect.dragSource(),
        isDragging: monitor.isDragging(),
    }),
)(VisiblePropsPanelItem),
)

but when I try to run it, I get: Uncaught Invariant Violation: Could not find the drag and drop manager in the context of DragSource (Component). Make sure you have your app with DragDropContext.

How can i fix it?

The code is taken from here: https://github.com/react-dnd/react-dnd/blob/master/packages/examples/src/04-sortable/simple/Card.tsx

Ad

Answer

You need to wrap your root component with dragDropContext or dragDropContextProvider.

For example:

import React, { Component } from "react";
// import ReactDOM from "react-dom";
import HTML5Backend from "react-dnd-html5-backend";
import { DragDropContext as dragDropContext } from "react-dnd";
import DragBox from "./DragBox";
import DropBox from "./DropBox";

class MainContext extends Component {
  render() {
    return (
      <div style={{ borderStyle: "solid" }}>
        <p style={{ padding: "0px 10px" }}>Main Context</p>
        <DragBox />
        <DropBox />
      </div>
    );
  }
}

module.exports = dragDropContext(HTML5Backend)(MainContext);

Full example

Ad
source: stackoverflow.com
Ad