Skip to content

Instantly share code, notes, and snippets.

@rcdexta
Last active October 16, 2019 06:15
Show Gist options
  • Save rcdexta/d710decab4885cdfa13d7e90a51b19f1 to your computer and use it in GitHub Desktop.
Save rcdexta/d710decab4885cdfa13d7e90a51b19f1 to your computer and use it in GitHub Desktop.
import React from "react";
import {SortableContainer, SortableElement, SortableHandle, arrayMove} from "react-sortable-hoc";
const DragHandle = SortableHandle(() => (
<span className="handle">&#x2261;</span>
));
const SortableItem = SortableElement(({ value }) => (
<li>
<DragHandle />
{value}
</li>
));
const SortableList = SortableContainer(({ items }) => {
return (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
);
});
class ItemList extends React.Component {
state = {
items: this.props.items
};
onSortEnd = ({ oldIndex, newIndex }) => {
this.setState({
items: arrayMove(this.state.items, oldIndex, newIndex)
});
};
render() {
return <SortableList items={this.state.items} onSortEnd={this.onSortEnd} />;
}
}
@jamesjara
Copy link

line:27 is wrong, anti pattern copying props unconditionally directly to the state cause bugs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment