Last active
October 16, 2019 06:15
-
-
Save rcdexta/d710decab4885cdfa13d7e90a51b19f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import {SortableContainer, SortableElement, SortableHandle, arrayMove} from "react-sortable-hoc"; | |
const DragHandle = SortableHandle(() => ( | |
<span className="handle">≡</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} />; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line:27 is wrong, anti pattern copying props unconditionally directly to the state cause bugs