[英]React DND - canDrag depending on component state
I'm facing a problem doing stuff with React-DND. 我在使用React-DND时遇到了问题。
My question is about preventing dragging a component for a specific state. 我的问题是关于阻止拖动特定状态的组件。 But "canDrag" can only have props and monitor as Parameters.
但是“canDrag”只能有道具和监视器作为参数。 (monitor.getItem shows null in my case) and I cannot use the props because these will be present in every component (since the props came from the parent component).
(monitor.getItem在我的例子中显示为null)并且我不能使用props,因为这些将出现在每个组件中(因为props来自父组件)。
Does anyone has any idea solving this problem? 有没有人有任何想法解决这个问题?
Thx 谢谢
You can do it in component rather then in spec 您可以在组件中而不是在规范中执行此操作
@DragSource(dragtype, sourceSpec, cnt => ({
connectDragSource: cnt.dragSource(),
}))
export default class Dragable extends React.Component {
state = {
canDrag: false,
}
render() {
const { connectDragSource } = this.props;
const { canDrag } = this.state;
const cntDragSource = canDrag ? connectDragSource : i => i;
return cntDragSource(
<div className="drag-target" />
)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.