无法分配给 'MyComponent' 因为它不是变量(ReactJS 中的 react-dnd 多拖放支持

Cannot assign to 'MyComponent' because it is not a variable (react-dnd multi drag&drop support in ReactJS

我想使用 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 名称,不如使用新名称:

class MyComponent extends ...

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

唯一重要的是您最终 export default 您希望调用者在导入时使用的版本 MyComponent