I am trying to use React DnD with a number of different draggable elements (different shapes, etc). They are all basically the same and have the same behaviour, so I figured using HOCs would be a good idea.
I have the following HOC that is a draggable component via React DnD:
import React, { Component } from "react";
import { DragSource } from "react-dnd";
// Components
import ItemTypes from "./ItemTypes";
/**
* Implements the drag source contract.
*/
const itemSource = {
beginDrag(props) {
return {};
}
};
/**
* Specifies the props to inject into your component.
*/
function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
};
}
const DragItem = WrappedComponent => {
return class extends Component {
render() {
const { isDragging, connectDragSource } = this.props;
return connectDragSource(<WrappedComponent isDragging={isDragging} {...this.props} />);
}
};
};
export default DragSource(ItemTypes.BOX, itemSource, collect)(DragItem);
I then have the base element that should implement the draggable HOC:
import React, { Component } from "react";
import DragItem from "../DragItem";
class Box extends Component {
render() {
return (
<div
style={{
height: "50px",
width: "50px",
background: "red"
}}
/>
);
}
}
export default DragItem(Box);
And here is the DnD context that ties them both together:
import React, { Component } from "react";
import { DragDropContext } from "react-dnd";
import HTML5Backend from "react-dnd-html5-backend";
// Components
import DropContainer from "./dragNDropUploader/DropContainer";
import Box from "./dragNDropUploader/types/Box";
class UploadTest extends Component {
render() {
return (
<div className="body_container padded_top padded_bottom">
<DropContainer />
<Box />
</div>
);
}
}
export default DragDropContext(HTML5Backend)(UploadTest);
I get the following in the dev tools console: react.development.js:233 Uncaught TypeError: Cannot set property 'props' of undefined
I'm not entirely sure what I'm doing wrong. If I remove the DnD stuff from the DragItem
HOC, things display as expected (however not draggable of course). But if I try to implement DnD as I have, it breaks and dies.
Please school me :)