共计 865 个字符,预计需要花费 3 分钟才能阅读完成。
可实现程度、垂直拖拽排序的 react 库
Github
https://github.com/clauderic/react-sortable-hoc
官网 demo
demo
装置
npm install react-sortable-hoc --save
根本应用
import React, {Component} from 'react';
import {render} from 'react-dom';
import {SortableContainer, SortableElement} from 'react-sortable-hoc';
import arrayMove from 'array-move';
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>
);
});
class SortableComponent extends Component {
state = {items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6'],
};
onSortEnd = ({oldIndex, newIndex}) => {this.setState(({items}) => ({items: arrayMove(items, oldIndex, newIndex),
}));
};
render() {return <SortableList items={this.state.items} onSortEnd={this.onSortEnd} />;
}
}
render(<SortableComponent />, document.getElementById('root'));
正文完