【问题标题】:React Beautiful DND not working, element is not draggableReact Beautiful DND 不起作用,元素不可拖动
【发布时间】:2020-09-27 02:07:15
【问题描述】:

我一直在尝试使用 react-beautiful-dnd 库在我的卡片中实现我的重新排序列表,但我以类似的方式尝试了 egghead.io 课程中的所有内容,但无法使其正常工作。这是我的代码:

    import React, { Component } from "react";
import { Card, Badge } from "react-bootstrap";
import "./projects.scss";
import projectInfo1 from "../../jsonData/projects1";
import { IconContext } from "react-icons";
import { FiPlus } from "react-icons/fi";
import { Droppable, DragDropContext, Draggable } from "react-beautiful-dnd";

class Projects extends Component {
  constructor(props) {
    super(props);
    this.state = {};
    this.onDragEnd = this.onDragEnd.bind(this);
    this.onDragStart = this.onDragStart.bind(this);
  }
  onDragEnd = result => {};
  onDragStart = result => {};

  render() {
    return (
      <div className="projectCards">
        {projectInfo1.projectsOrder.map((projectID) => {
          const project = projectInfo1.projects[projectID];
          return (
            <DragDropContext onDragEnd={this.onDragEnd} onDragStart={this.onDragStart}>
            <Card className="projectCard" bg="light" style={{ width: "21rem" }} key={project.id}>
                <Card.Header color="#366FF0" className="projectcardheader">
                  {project.projectName}
                </Card.Header>
                <Droppable droppableId={project.id}>
                  {(provided) => (
                    <div
                      className="cardcontent"
                      innerRef={provided.innerRef}
                      {...provided.droppableProps}
                    >
                      {project.topics.map((j, index) => {
                        return (
                          <Draggable draggableId={j.id} index={index}>
                            {(provided) => (
                              <Card
                                key={j.id}
                                className="topicscard"
                                {...provided.draggableProps}
                                {...provided.dragHandleProps}
                                innerRef={provided.innerRef}
                              >
                                <Card.Title className="topicsheading">
                                  {j.topicName}
                                </Card.Title>
                                <Card.Text className="topicdescription">
                                  {j.topicDescription}
                                </Card.Text>
                                <div>
                                  {j.topicTags ? (
                                    j.topicTags.map((k) => {
                                      return (
                                        <Badge
                                          variant="primary"
                                          className="tags"
                                        >
                                          {k}
                                        </Badge>
                                      );
                                    })
                                  ) : (
                                    <Badge variant="primary"></Badge>
                                  )}
                                </div>
                              </Card>
                            )}

                          </Draggable>
                        );
                      })}
                      {provided.placeholder}
                    </div>
                  )}
                </Droppable>
                <div className="addnewcard">
                  <IconContext.Provider
                    value={{
                      style: { verticalAlign: "middle" },
                      className: "reacticon",
                    }}
                  >
                    <FiPlus />
                  </IconContext.Provider>{" "}
                  Add another discussion
                </div>
            </Card>
            </DragDropContext>
          );
        })}
      </div>
    );
  }
}
export default Projects;

我还附上了一张照片,它只是显示拖动光标悬停在卡片上,但拖动时没有任何反应,任何帮助都会救我!

const projectsInfo1 = {

    projectsOrder:['Project-2','Project-1','Project-5','Project-4','Project-3'],
    projects: {
      'Project-1':{
        projectName: "Project 1",
        id:"p1",
        topics:[{
            id:"p1t1",
            topicName: "Adding a feature: GSoC1",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content.",      
            topicTags:['ReactJs','NodeJS']
        },
        {   id:'p1t2',
            topicName: "Adding a feature: GSoC2",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content.",
            topicTags:['ReactJs','NodeJS']     
        },
        {   id:'p1t3',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content.",
            topicTags:['ReactJs','NodeJS']      
        },
        {   id: 'p1t4',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content.",
            topicTags:['ReactJs','NodeJS']      
        }],
      },
      'Project-2':{
        projectName: "Project 2",
        id:'p2',
        topics:[{
            id:'p2t1',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p2t2',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p2t3',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p2t4',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        }],
      },
      'Project-3':{
        projectName: "Project 3",
        id:'p3',
        topics:[{
            id:'p3t1',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p3t2',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p3t3',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p3t4',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        }],
      },
      'Project-4':{
        projectName: "Project 4",
        id:'p4',
        topics:[{
            id:'p4t1',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p4t2',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p4t3',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p4t4',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        }],
      },
      'Project-5':{
        projectName: "Project 5",
        id:'p5',
        topics:[{
            id:'p5t1',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p5t2',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p5t3',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        },
        {   id:'p5t4',
            topicName: "Adding a feature: GSoC",
            topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content."      
        }],
      },
    },
  };

  export default projectsInfo1;

【问题讨论】:

    标签: reactjs react-beautiful-dnd reorderlist


    【解决方案1】:

    注意:

    在评论对话后,问题是innerRef

    &lt;div innerRef={provided.innerRef} 到 &lt;div ref={provided.innerRef}

    在演示中他们使用样式化组件,所以他们使用innerRef,但如果您使用简单的 div,那么您应该只使用ref


    问题:

    1)它不会自动工作,你需要在拖拽结束事件上为它写代码onDragEnd

    2) 没有状态管理,所以即使你对导入的 json 进行了更改,也不会反映为 react 不知道要重新渲染 dom。


    解决方案:

    1) 将您导入的数据保存在state

    2) 在onDragEnd 中编写一些代码,检查那里的所有条件并相应地对您的状态应用更改(不要改变状态,否则它不会反映更改)

    注意:以下只是 egghead.io 中用于排序的 demo 的代码 sn-p 在同一个可放置区域内列出,只需大致了解它是如何实现的 应该是

    onDragEnd = result => {
      const { destination, source, draggableId } = result
    
      if (!destination) {
        return
      }
    
      if (
        destination.droppableId === source.droppableId &&
        destination.index === source.index
      ) {
        return
      }
    
      const start = this.state.columns[source.droppableId]
      const finish = this.state.columns[destination.droppableId]
    
    
      // this is the logic behind sorting state , you have to do it by your self
      if (start === finish) {
        const newTaskIds = Array.from(start.taskIds)
        newTaskIds.splice(source.index, 1)
        newTaskIds.splice(destination.index, 0, draggableId)
    
        const newColumn = {
          ...start,
          taskIds: newTaskIds
        }
    
        const newState = {
          ...this.state,
          columns: {
            ...this.state.columns,
            [newColumn.id]: newColumn
          }
        }
    
        this.setState(newState)
        return
      }
    }
    

    工作演示:

    【讨论】:

    • 不,就像在这个视频中你可以看到的,egghead.io/lessons/… 它为他工作,即使他还没有编写 onDragEnd 的逻辑,我和我一样,虽然我在导入数据的同时编写了它在状态,仍然没有变化:(/
    • 如果你看到视频,item 只是可拖动的,但它不是 ordering ,只要他放下元素,它就会恢复到原始位置
    • 我的甚至没有从它的位置抬起。就像它不动!它只是在悬停时显示拖动光标图标
    • @Devansh_Geek,尝试将&lt;div innerRef={provided.innerRef}更改为&lt;div ref={provided.innerRef}并再次检查
    • 是的,即使使用样式组件,您似乎也必须使用 ref={provided.innerRef}。我在这个上浪费了很多时间。我当然希望在 egghead.io 上更新教程
    猜你喜欢
    • 2020-09-27
    • 2020-12-04
    • 2019-04-29
    • 2019-06-06
    • 2020-05-05
    • 2022-11-03
    • 2022-08-24
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多