react-sortable-hoc -- 在没有实验性 class 属性的情况下维护新的排序顺序
react-sortable-hoc -- maintain new sort order without experimental class properties
以下配置未引发任何错误,但未维护新的排序顺序。拖动和关联的动画运行良好,但排序顺序本身永远不会永久更改。
我已根据 https://www.npmjs.com/package/react-sortable-hoc 中的示例对我的代码进行了轻微修改。 (我将一些代码移到了构造函数中,以解决与实验性 class 属性相关的错误。)
有什么想法吗?
import {
SortableContainer,
SortableElement,
arrayMove,
} from 'react-sortable-hoc';
const SortableItem = SortableElement(({value}) => <li>{value}</li>);
const SortableList = SortableContainer(({items}) => {
return (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
);
});
class SortableComponent extends Component {
constructor(props) {
super(props);
this.state = {};
this.state.items = [
"Gold",
"Crimson",
"Hotpink",
"Blueviolet",
"Cornflowerblue",
"Skyblue",
"Lightblue",
"Aquamarine",
"Burlywood"
];
this.onSortEnd = this.onSortEnd.bind(this);
}
onSortEnd(oldIndex, newIndex) {
this.setState(({items}) => ({
items: arrayMove(items, oldIndex, newIndex),
}));
}
render() {
return <SortableList items={this.state.items} onSortEnd={this.onSortEnd} />;
}
}
问题出在 onSortEnd 回调中:
onSortEnd(oldIndex, newIndex) {
this.setState(({items}) => ({
items: arrayMove(items, oldIndex, newIndex),
}));
}
您需要将函数 (oldIndex, newIndex)
的参数更改为 ({ oldIndex, newIndex })
来自 github 文档 (https://github.com/clauderic/react-sortable-hoc):
onSortEnd - 排序结束时调用的回调。 function({oldIndex, newIndex, collection}, e)
请注意函数签名和您的实现之间的差异,oldIndex
和 newIndex
是通过在第一个参数上使用对象解构来分配的。通过使用函数的第二个参数作为 oldIndex
它实际上将 e
作为值,这显然不能用作更新数组顺序时的索引!
以下配置未引发任何错误,但未维护新的排序顺序。拖动和关联的动画运行良好,但排序顺序本身永远不会永久更改。
我已根据 https://www.npmjs.com/package/react-sortable-hoc 中的示例对我的代码进行了轻微修改。 (我将一些代码移到了构造函数中,以解决与实验性 class 属性相关的错误。)
有什么想法吗?
import {
SortableContainer,
SortableElement,
arrayMove,
} from 'react-sortable-hoc';
const SortableItem = SortableElement(({value}) => <li>{value}</li>);
const SortableList = SortableContainer(({items}) => {
return (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
);
});
class SortableComponent extends Component {
constructor(props) {
super(props);
this.state = {};
this.state.items = [
"Gold",
"Crimson",
"Hotpink",
"Blueviolet",
"Cornflowerblue",
"Skyblue",
"Lightblue",
"Aquamarine",
"Burlywood"
];
this.onSortEnd = this.onSortEnd.bind(this);
}
onSortEnd(oldIndex, newIndex) {
this.setState(({items}) => ({
items: arrayMove(items, oldIndex, newIndex),
}));
}
render() {
return <SortableList items={this.state.items} onSortEnd={this.onSortEnd} />;
}
}
问题出在 onSortEnd 回调中:
onSortEnd(oldIndex, newIndex) {
this.setState(({items}) => ({
items: arrayMove(items, oldIndex, newIndex),
}));
}
您需要将函数 (oldIndex, newIndex)
的参数更改为 ({ oldIndex, newIndex })
来自 github 文档 (https://github.com/clauderic/react-sortable-hoc):
onSortEnd - 排序结束时调用的回调。 function({oldIndex, newIndex, collection}, e)
请注意函数签名和您的实现之间的差异,oldIndex
和 newIndex
是通过在第一个参数上使用对象解构来分配的。通过使用函数的第二个参数作为 oldIndex
它实际上将 e
作为值,这显然不能用作更新数组顺序时的索引!