如何使用不可变表示此数组重新排序示例

How to represent this array re-ordering example using Immutable?

本文关键字:排序 新排序 不可变 何使用 表示 数组      更新时间:2023-09-26

来自 react-dnd 的代码示例:

https://github.com/gaearon/react-dnd/blob/master/examples/04%20Sortable/Simple/Container.js#L46-L53

this.setState(update(this.state, {
  cards: {
    $splice: [
      [dragIndex, 1],
      [hoverIndex, 0, dragCard]
    ]
  }
}));

我正在尝试使用不可变执行相同的操作,但无法弄清楚如何执行。

知道了!

const cards = state.get('cards');
const newState = state.set('cards',
  cards.splice(dragIndex, 1)
  .splice(hoverIndex, 0, dragCard));