【发布时间】:2021-07-13 11:36:35
【问题描述】:
我正在尝试使用 React Beautiful DND 库在待办事项应用程序中拖放列表,但我在控制台中不断收到这三个错误。
错误 1:
Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Check the render method of `Droppable`.
错误 2:
Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Check the render method of `Draggable`.
错误 3:
Invariant failed: provided.innerRef has not been provided with a HTMLElement.
以下是我的代码的sn-p。我还使用了样式化组件:
import React from 'react';
import styled from 'styled-components';
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
function App() {
...
return (
<>
<DragDropContext>
<Droppable>
{(provided) => (
<TodoList {...provided.droppableProps} ref={provided.innerRef}>
{
shadowTodoItems.map((todoItem, index) => (
<Draggable key={todoItem.id} draggableId={todoItem.id} index={index}>
{(provided) => (
<div>
<TodoListItem ref={provided.innerRef} {...provided.draggableProps} {...provided.dragHandleProps} />
</div>
)}
</Draggable>
))
}
{provided.placeholder}
</TodoList>
)}
</Droppable>
</DragDropContext>
</>
);
}
请问我该如何解决这些错误?谢谢。
【问题讨论】:
-
错误中提到,需要将ref转发到
TodoList
标签: javascript reactjs react-beautiful-dnd react-ref