【问题标题】:connectDragSource for Each Element in map (Drag and Drop: React DnD)地图中每个元素的 connectDragSource(拖放:React DnD)
【发布时间】: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


    【解决方案1】:

    您可能希望创建一个单独的组件来呈现每个项目并使其成为拖动源。您还需要从规范中的 canDrag 函数返回 true。

    免责声明:我没有测试过下面的代码。

    shapes.js

    import React, { Component } from "react";
    import Item from './item.js';
    
    class Shapes extends Component {    
        render() {
            const { shapes } = this.props;
            return (
                <div>
                    {
                        shapes.length > 0 && 
                        shapes.map((shape) => <Item key={shape.id} shape={shape} />)
                    }
                </div>
            );
        }
    }
    
    export default Shapes;
    

    item.js

    import React, { Component } from "react";
    import { Segment, Icon } from "semantic-ui-react";
    import { DragSource } from "react-dnd";
    
    class Item extends Component {    
        render() {
            const { shape, connectDragSource} = this.props;
            return connectDragSource(
                <div>
                     <Segment>
                         <Icon name={shape.name} size={shape.size} color={shape.color} />
                     </Segment>
                </div>
            )
        }
    }
    
    const spec = {
        beginDrag(props) {
            return {
                shapeId: props.shapes.id
            };
        },
        canDrag(props, monitor) {
            return true;
        },
        endDrag(props, monitor) {
            console.log("You dropped .... into " + monitor.getDropResult());
        }
    };
    
    const collect = (connect, monitor) => ({
      connectDragSource: connect.dragSource(spec),
      isDragging: monitor.isDragging()
    });
    
    export default DragSource("shape", spec, collect)(Item);
    

    【讨论】:

    • 非常感谢!
    • @31113 -- 没问题。很高兴我能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 2019-09-09
    • 2019-06-06
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多