• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    미해결

delete 시 slice extraReducer에서 id는 어떻게 받나요?

21.12.18 15:13 작성 조회수 118

0

createAsyncThunk에서 삭제 api를 보내고 응답값은 보통 statusCode와 삭제메시지정도인데... slice의 extraReducer에서 id를 어떻게 받아서 state에서 삭제하는 방법이 무엇인지요??

export const deleteItem = createAsyncThunk(
`item/deleteItem`,
async (id, thunkAPI) => {
const response = await axios.delete(
`/api/items/${id}`,
tokenConfig(thunkAPI.getState)
);
return response.data;
}
 
const itemSlice = createSlice({
name: `item`,
initialState: { items: [], isLoading: false },
reducers: {},
extraReducers: {
[getItems.fulfilled]: (state, action) => {
state.items = action.payload;
state.isLoading = false;
},

[addItem.fulfilled]: (state, action) => {
state.items.push(action.payload);
state.isLoading = false;
},

[deleteItem.fulfilled]: (state, action) => {
id???
},
},
});

답변 1

답변을 작성해보세요.

0

action.meta.arg에 id가 들어있을 겁니다. asyncThunk에서 인자로 받았던 게 action.meta.arg로 이어집니다.