我想使用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中实现相同的目的?
答案 0 :(得分:1)
不要尝试重新分配您的班级名称,而只需使用新名称即可:
class MyComponent extends ...
const MyComponent2 = DragSource(...)(MyComponent);
const MyComponent3 = DropTarget(...)(MyComponent2);
export default MyComponent3;
唯一重要的是,您最终export default
是呼叫者导入MyComponent
时要使用的版本。