【发布时间】:2018-12-17 01:15:19
【问题描述】:
如何使每个项目都可以放置?
当map() 函数返回新数组时,我决定将connectDragSource 传递给该方法,但我仍然返回 droppable Aray insted droppable Each Item(形状)
for..in, forEach, for..of 也没有得到想要的结果
有人解决了这个问题吗?
import React, { Component } from "react";
import { Segment, Icon } from "semantic-ui-react";
import { DragSource } from "react-dnd";
import ShapeItem from "./ShapeItem";
class Shapes extends Component {
displayShapes = (shapes, connectDragSource) =>
shapes.length > 0 &&
shapes.map((shape) =>
connectDragSource(
<div key={shape.id}>
<Segment>
<Icon name={shape.name} size={shape.size} color={shape.color} />
</Segment>
</div>
)
);
render() {
const { shapes, connectDragSource} = this.props;
return (
<div>
{this.displayShapes(shapes, connectDragSource)}
</div>
);
}
}
const spec = {
beginDrag(props) {
return {
shapeId: props.shapes.id
};
}
};
const collect = (connect, monitor) => ({
connectDragSource: connect.dragSource(spec),
isDragging: monitor.isDragging()
});
export default DragSource("shape", spec, collect)(Shapes);
至于文档http://react-dnd.github.io 仅找到以下内容:将元素作为可拖动节点。为此,请将渲染函数中的任何element 替换为this.props.connectDragSource(element)。
【问题讨论】:
标签: reactjs drag-and-drop draggable react-dnd