在Reducer中更改数组的索引

时间:2019-09-18 17:08:31

标签: javascript reactjs redux

我想在reducer.js中移动数组的索引。因此,我正在使用一个名为arrayMove的函数来移动我的减速器的索引。其实我有这段代码:

reducer.js

case TYPES.MOVE_UP_ORDEM_INSUMOS:
  function arrayMove(arr, fromIndex, toIndex) {
    var element = arr[fromIndex];
    arr.splice(fromIndex, 1);
    arr.splice(toIndex, 0, element);
  }
  return Object.assign({}, state, {
    insumos: arrayMove(state.insumos, payload[0], payload[1]),
  });

component.js

moveUpOrder = (item, index) => {
  let ordem = index - 1;
  this.props.thunks.moveUpInsumo(index, ordem);
};

actions.js

export const moveUpIndexInsumo = (fromIndex, toIndex) => ({
 type: TYPES.MOVE_UP_ORDEM_INSUMOS,
 payload: [fromIndex, toIndex],
});

thunks.js

export const moveUpInsumo = (fromIndex, toIndex) => dispatch => {
 dispatch(actions.moveUpIndexInsumo(fromIndex, toIndex));
};

但是它不起作用。该函数可以移动,但是我的称为insumos的全局状态不会移动。

0 个答案:

没有答案