在react-dnd中找不到DragSource
我正在学习 React 中的拖放操作,但我不断收到此错误(参见图片),即 React-dnd 中不存在 DragSource。我已经使用“npm install react-dnd”安装了react-dnd。在我所做的研究中,我还没有看到这个错误的任何答案。
代码如下:
import React, {Component} from 'react'
import {DragSource} from 'react-dnd'
const itemSource = {
beginDrag(props) {
return props.item
},
endDrag(props, monitor, component) {
return props.handleDrop(props.item.id)
}
}
function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
connectDragPreview: connect.dragPreview(),
isDragging: monitor.isDragging()
}
}
class Item extends Component {
render() {
const { isDragging, connectDragSource, item } = this.props;
return connectDragSource(
<div className='item'>
<span>{item.name}</span>
</div>
)
}
}
export default DragSource('item', itemSource, collect)(Item)
I'm learning about drag and drop in react, but I keep getting this error (see image) that DragSource doesn't exist in react-dnd. I have installed react-dnd using "npm install react-dnd". I haven't seen any answer to this bug in my of the research I did.
react-dnd package in node_modules
Code below:
import React, {Component} from 'react'
import {DragSource} from 'react-dnd'
const itemSource = {
beginDrag(props) {
return props.item
},
endDrag(props, monitor, component) {
return props.handleDrop(props.item.id)
}
}
function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
connectDragPreview: connect.dragPreview(),
isDragging: monitor.isDragging()
}
}
class Item extends Component {
render() {
const { isDragging, connectDragSource, item } = this.props;
return connectDragSource(
<div className='item'>
<span>{item.name}</span>
</div>
)
}
}
export default DragSource('item', itemSource, collect)(Item)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据react-dnd release v15.0.0
装饰器 API 包括 DragSource 和 DropTarget。
它们可以被react-dnd 中的useDrag 和useDrop 钩子替换。
According to react-dnd release v15.0.0
The Decorators API included DragSource and DropTarget.
They can be replaced by useDrag and useDrop hooks from react-dnd.