react/reduxとredux-sagaミドルウェアを使用して、アイテムを配列に追加するときに自分の状態を更新しようとしています。しかし、私がこれを行う場所では、各アイテムにインデックス(0,1,2 ...)がある配列がありますが、新しいアイテム(ステップという名前)を追加すると、新しいアイテムにはインデックスがありません減速さインデックス付きの配列にアイテムを追加する - reactxとredux-saga
[
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#merge)は配列ではなくオブジェクトで動作します –