【问题标题】:React-Beautiful-Dnd Can't Find Draggable Element with IdReact-Beautiful-Dnd 找不到带有 Id 的可拖动元素
【发布时间】:2020-12-04 14:22:46
【问题描述】:

我正在尝试复制 react-beautiful-dnd 教程第 4 步:重新排序列表。据我所知,我已经复制了教程中的代码:https://egghead.io/lessons/react-reorder-a-list-with-react-beautiful-dnd

但是,当我运行 react 并尝试拖动列表项时,出现如下错误:Unable to find draggable element with id: task-1

如果我查看 DOM,我肯定可以看到具有该 id 的元素:

我不知道我做错了什么。我将 id 打印到控制台以检查它是否是字符串,并且确实如此。想法?

INITIAL-DATA.JS

const initialData = {

    tasks : {
        "task-1" : { id : "task-1", content : "Take out garbage"},
        "task-2" : { id : "task-2", content : "Watch show"},
        "task-3" : { id : "task-3", content : "Charge phone"},
        "task-4" : { id : "task-4", content : "Cook dinner"},
    },

    columns : {
        "column-1" : {
            id : "column-1",
            title: "To Do",
            taskIds : ["task-1", "task-2", "task-3", "task-4"]
        }
    },

    columnOrder : ["column-1"]
};

export default initialData;

INDEX.JS

import React from 'react';
import ReactDOM from 'react-dom';
import initialData from "./initial-data";
import Column from "./column";
import { DragDropContext } from 'react-beautiful-dnd';

class App extends React.Component {
    state = initialData;

    // Needs to synchronously update our state to reflect the drag-drop result
    // The only required DragDrop callback
    onDragEnd = result => {

    }
        
    render() {
        return (
            <DragDropContext onDragEnd={this.onDragEnd}>
            {
                this.state.columnOrder.map( (columnId) => {
            
                    const column = this.state.columns[columnId];
                    const tasks = column.taskIds.map( taskId => this.state.tasks[taskId]);

                    return <Column key={column.id} column={column} tasks={tasks} />;

                })
            }
            </DragDropContext>
        )
    }
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

COLUMN.JS

import React from "react";
import styled from "styled-components";
import Task from "./task";
import { Droppable } from 'react-beautiful-dnd';

const Container = styled.div`
    margin: 8px;
    border: 1px solid lightgrey;
    border-radius: 2px;
`;
const Title = styled.h3`
    padding: 8px;
    margin: 0px;
`;
const TaskList = styled.div`
    padding: 8px;
`;

export default class Column extends React.Component {
    render() {
        return (
            // Note: Droppables expect their child to be a function that returns a react component
            <Container>
                <Title>{this.props.column.title}</Title>
                <Droppable droppableId={this.props.column.id}> 
                { provided => (
                    // The droppableProps in the provided object (a react-beautiful-dnd object) need to get provided to the object
                    // you want to designate as your droppable
                    // {provided.placeholder} // Needs to be added as a child of the component you designate as the droppable
                    // ref is an attribute of -components. Returns the dom node of the component
                    <TaskList 
                        ref={provided.innerRef}
                        {...provided.droppableProps}
                    >
                        { this.props.tasks.map( (task, index) => <Task key={task.id} task={task} index={index} /> ) }
                        {provided.placeholder}
                    </TaskList>
                )}
                </Droppable>
            </Container>
        )
    }
}

TASK.JS

import React from "react";
import styled from "styled-components";
import { Draggable } from 'react-beautiful-dnd';

const Container = styled.div`
    border: 1px solid lightgrey;
    border-radius: 2px;
    padding: 8px;
    margin-bottom: 8px;
    background-color: white; /* so don't see through when dragging */
`;

export default class Task extends React.Component {
    render() {
        console.log(this.props.task.id)
        console.log(typeof this.props.task.id)
        return (
            // Required draggable props are draggableId and index
            // Note: Draggables expect their child to be a function that returns a react component
            <Draggable draggableId={this.props.task.id} index={this.props.index}>
                { provided => (
                    // The draggbleProps in the provided object (a react-beautiful-dnd object) need to get provided to the object
                    // you want to designate as your draggable
                    // DragHandleProps needs to get applied to the part of that object that you want to use to drag the whole object
                    // ref is an attribute of -components. Returns the dom node of the component
                    <Container
                        ref={provided.innerRef}
                        {...provided.draggbleProps}
                        {...provided.dragHandleProps}
                    >
                        { this.props.task.content }
                    </Container>
                )}
            </Draggable>
        )
    }
}

【问题讨论】:

    标签: reactjs draggable react-beautiful-dnd


    【解决方案1】:

    您的代码中只有一个错字:在 task.js 中将 {...provided.draggbleProps} 更改为 {...provided.draggableProps}

    【讨论】:

    • 谢谢!我是 React 和 Visual Studio Code 的新手,有没有办法告诉我是否有这样的错字?我知道我用过的其他 IDE 会在某个函数不存在时说,但这似乎不存在。
    【解决方案2】:

    如上所示,这里的问题是错字。根据您在该答案下方的评论,您将来可以通过使用 Typescript 来帮助避免这种情况。它会在你的编辑器中显示一个错字,并自动完成。

    【讨论】:

    • 这很容易成为评论而不是问题。
    • 我还不能发表评论 :( 我可以评论我自己的答案,但我没有评论其他问题或答案的权限......
    • @EmilyJablonski 谢谢你的建议!关于答案与评论,您可能会做的是,一旦您有足够的声誉(不需要很长时间),重复您在此处写的内容作为对相关评论的评论回复,然后删除答案本身。这更合适,因为您的回答是关于评论中提出的问题,而不是真正的主要问题本身。
    猜你喜欢
    • 2020-09-27
    • 2019-06-06
    • 2022-09-24
    • 2020-09-27
    • 2019-04-29
    • 2022-11-03
    • 2020-05-05
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多