我嘗試更新我的狀態時添加一個項目在數組中,使用react/redux和REDEX傳奇中間件。但是,在我這樣做的地方,我有一個數組,其中每個項目都有一個索引(0,1,2 ....),但是當我添加新項目(名爲step)時,新項目沒有索引,但它是這樣的:添加與索引數組中的項目 - 反應REDX和REDX傳奇
[
0: item1,
1: item2,
2: item3,
step: newItem
]
而且我想,新項目有一個索引like 3: newItem
這裏是減速機:
export function stepReducer(state = [], action) {
switch (action.type) {
case Actions.CREATE_STEP_REQUEST:
return state;
case Actions.STEP_CREATED:
let newArray = action.steps.slice();
newArray.splice(action.index, 0, action.step);
return newArray;
case Actions.FETCH_TRAVEL_STEPS_REQUIRED:
return state;
case Actions.TRAVEL_STEPS_DATA_SUCCESS:
let updatedSteps = _.merge(action.steps, state.steps);
return updatedSteps;
default:
return state;
}
}
和傳奇文件:
export function* createStep(action) {
const step = yield call(stepApi.createStep, action.travelId, action.step, action.authToken);
const steps = yield select((state) => state.steps);
const index = steps.length;
yield put({ type: Actions.STEP_CREATED, steps, step, index });
}
如果有人應該幫忙,
謝謝!
[_.merge](https://lodash.com/docs/4.17 .4#合併)與對象不是數組一起工作 –