首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-dnd的useDrag方法无法在typescript中获取isDragging

react-dnd的useDrag方法无法在typescript中获取isDragging
EN

Stack Overflow用户
提问于 2021-11-14 13:22:31
回答 1查看 235关注 0票数 0

我是使用typescriptreact的新手,我正在使用typescriptreact-dnd包实现拖放功能。根据此blog,我尝试拖动图像,但我面临一个问题

类型unknown上不存在属性isDragging

代码:

代码语言:javascript
复制
const [{ isDragging }, drag] = useDrag({
        // item denotes the element type, unique identifier (id) and the index (position)
        item: { type, id: image.id, index },
        // collect method is like an event listener, it monitors whether the element is dragged and expose that information
        collect: monitor => ({
          isDragging: monitor.isDragging()
        })
      });

我得到的错误是:

我对isDragging的设置类型感到困惑,或者如何解决这个错误?

EN

回答 1

Stack Overflow用户

发布于 2021-11-15 06:30:28

从react-dnd TSX示例中获得答案

sandbox url

代码语言:javascript
复制
const [{ isDragging }, drag] = useDrag({
        type: "image",
        item: () => {
          return { id, index }
        },
        collect: (monitor: any) => ({
          isDragging: monitor.isDragging(),
        }),
      })

类型不是可选的,我们需要为其定义类型

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69963411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档