gpt4 book ai didi

javascript - 如何将装饰器语法转换为 ES6?

转载 作者:行者123 更新时间:2023-11-29 21:02:25 25 4
gpt4 key购买 nike

我正在努力理解 some code用 ESnext(装饰器)编写的 React。我知道如何将装饰器从 ESnext 语法转换为 ES6 语法

// ESnext
function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
}
}

@DragSource(Types.CARD, cardSource, collect)
export default class Card extends React.Component {
render() {
const { id } = this.props;
const { isDragging, connectDragSource } = this.props;

return connectDragSource(
<div>
I am a draggable card number {id}
{isDragging && ' (and I am being dragged now)'}
</div>
);
}
}

ES6

// ES6
function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
};
}

class Card extends React.Component {
render() {
const { id } = this.props;
const { isDragging, connectDragSource } = this.props;

return connectDragSource(
<div>
I am a draggable card number {id}
{isDragging && ' (and I am being dragged now)'}
</div>
);
}
}

export default DragSource(Types.CARD, cardSource, collect)(Card);

但我不知道如何将此代码转换为 ES6?

function collectDrop(connect) {
return {
connectDropTarget: connect.dropTarget(),
};
}

function collectDrag(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
};
}

@DropTarget(ItemTypes.CARD, cardTarget, collectDrop)
@DragSource(ItemTypes.CARD, cardSource, collectDrag)
export default class Card extends Component {
static propTypes = {
connectDragSource: PropTypes.func.isRequired,
connectDropTarget: PropTypes.func.isRequired,
index: PropTypes.number.isRequired,
isDragging: PropTypes.bool.isRequired,
id: PropTypes.any.isRequired,
text: PropTypes.string.isRequired,
moveCard: PropTypes.func.isRequired,
};

render() {
const { text, isDragging, connectDragSource, connectDropTarget } = this.props;
const opacity = isDragging ? 0 : 1;

return connectDragSource(connectDropTarget(
<div style={{ ...style, opacity }}>
{text}
</div>,
));
}
}

最佳答案

因为您有两个高阶组件 (HOC) 装饰器,您需要将它们组合起来并在使用这两个(DropTarget 和 DragSource)导出时包装您的类。如果您正在使用 redux 库,那么您可以使用它的实用函数 compose 组合多个 HOC 并用它包装类。您需要关注的代码在下面代码的末尾:

import { compose } from 'redux'

function collectDrop(connect) {
return {
connectDropTarget: connect.dropTarget(),
};
}

function collectDrag(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
};
}

class Card extends Component {
static propTypes = {
connectDragSource: PropTypes.func.isRequired,
connectDropTarget: PropTypes.func.isRequired,
index: PropTypes.number.isRequired,
isDragging: PropTypes.bool.isRequired,
id: PropTypes.any.isRequired,
text: PropTypes.string.isRequired,
moveCard: PropTypes.func.isRequired,
};

render() {
const { text, isDragging, connectDragSource, connectDropTarget } = this.props;
const opacity = isDragging ? 0 : 1;

return connectDragSource(connectDropTarget(
<div style={{ ...style, opacity }}>
{text}
</div>,
));
}
}

const enhance = compose(
DropTarget(ItemTypes.CARD, cardTarget, collectDrop),
DragSource(ItemTypes.CARD, cardSource, collectDrag)
)

export default enhance(Card)

或者(如果你不使用 redux)你可以这样组合它们:

// Comment this part out
/* const enhance = compose(
DropTarget(ItemTypes.CARD, cardTarget, collectDrop),
DragSource(ItemTypes.CARD, cardSource, collectDrag)
)

export default enhance(Card)*/

// and change to this

const dropTargetHOC = DropTarget(ItemTypes.CARD, cardTarget, collectDrop)
const dragSourceHOC = DragSource(ItemTypes.CARD, cardSource, collectDrag)

export default dropTargetHOC(dragSourceHOC(Card))

关于javascript - 如何将装饰器语法转换为 ES6?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45863194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com