【发布时间】:2020-03-07 13:12:54
【问题描述】:
我想使用一个名为 React Sortable HOC 的 React 排序库。这似乎是一个很棒的库,但我在尝试弄清楚如何在我的实例中使用它时遇到了麻烦。
我按照他们提供的示例之一进行设置:
const SortableItem = SortableElement(({ value }) => <li>{value}</li>);
const SortableList = SortableContainer(({ items }) => {
return (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${value}`} index={index} value={value} />
))}
</ul>
);
});
const onSortEnd = ({ oldIndex, newIndex }) => {
this.setState(({ items }) => ({
items: arrayMove(items, oldIndex, newIndex),
}));
};
在示例中,他们从状态映射如下:
return (
<SortableContainer onSortEnd={this.onSortEnd}>
{items.map((value, index) => (
<SortableItem key={`item-${value}`} index={index} value={value} />
))}
</SortableContainer>
);
但就我而言,我正在使用“地图”来查看我的状态项(星舰)并生成如下数据:
return (
<div>
{starships.map((starShip) => (
<Ship starShip={starShip} />
))}
</div>
);
Ship 在哪里是这个元素:
const Ship = ({ ship: { ShipId, ShipName } }) => (
<tr key={ShipId}>
<td>{ShipNameName}</td>
</tr>
);
我不知道如何以我设置应用程序的方式使用这个库。
有人这样用过吗?
谢谢!
【问题讨论】:
标签: reactjs react-sortable-hoc