我想使用react-dnd同时拖放

// ... imports ...
class MyComponent extends Component {
  render () {
    const { connectDragSource, isDragging, connectDropTarget, isOver } = this.props

    return connectDragSource(connectDropTarget(
      <div style={{ opacity: isDragging ? 0.5 : 1 }} >
        Just an example
     </div>
    ))
  }
}

MyComponent = DragSource('MyComponent', elementSource, (connect,
monitor) => ({
  connectDragSource: connect.dragSource(),
  isDragging: monitor.isDragging()
}))(MyComponent);

MyComponent = DropTarget('MyComponent', elementTarget, connect => ({
  connectDropTarget: connect.dropTarget(),
}))(MyComponent);

export default MyComponent;


我发现here中有可能。但是,在我的环境中,我收到此错误:

Cannot assign to 'MyComponent' because it is not a variable [.ts(2539)]


我正在使用TypeScript(.ts)。这可能是问题吗?如果是这样,如何在TypeScript中实现相同的目的?

最佳答案

不必尝试重新分配您的类名称,而只需使用新名称即可:

class MyComponent extends ...

const MyComponent2 = DragSource(...)(MyComponent);
const MyComponent3 = DropTarget(...)(MyComponent2);
export default MyComponent3;


唯一重要的是最终您将呼叫者导入export default时最终使用的版本为MyComponent

关于javascript - 无法分配给“MyComponent”,因为它不是变量(ReactJS中的red-dnd多重拖放支持,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55693034/

10-09 16:44