I want to use a React sorting library called, React Sortable HOC. It seems like a great library, but I'm having trouble trying to figure out how to use it in my instance.
I set it up like one of the examples they provided:
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),
}));
};
In the example, they map it from state like this:
return (
<SortableContainer onSortEnd={this.onSortEnd}>
{items.map((value, index) => (
<SortableItem key={`item-${value}`} index={index} value={value} />
))}
</SortableContainer>
);
But in my case, I am using 'map' to go through my state item(starships) and generating my data like this:
return (
<div>
{starships.map((starShip) => (
<Ship starShip={starShip} />
))}
</div>
);
Where Ship is this element:
const Ship = ({ ship: { ShipId, ShipName } }) => (
<tr key={ShipId}>
<td>{ShipNameName}</td>
</tr>
);
I can't figure out how I'd use this library with the way I have my application set up.
Is there anyone who has used it like this?
Thanks!