Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6783bda

Browse files
committedNov 12, 2023
Переписан extrareducers на callback builder
1 parent f2f1d89 commit 6783bda

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed
 

‎src/slices/categoriesSlice.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ export const categoriesSlice = createSlice({
2626
return state.data.filter(todo => todo.id !== id);
2727
}
2828
},
29-
extraReducers: {
30-
[fetchCategories.pending]: (state) => {
31-
state.status = 'loading';
32-
},
33-
[fetchCategories.fulfilled]: (state, action) => {
34-
state.status = 'successful';
35-
state.data = action.payload;
36-
},
37-
[fetchCategories.rejected]: (state) => {
38-
state.status = 'failed';
39-
}
29+
extraReducers: (builder) => {
30+
builder
31+
.addCase(fetchCategories.pending, (state) => {
32+
state.status = 'loading';
33+
})
34+
.addCase(fetchCategories.fulfilled, (state, action) => {
35+
state.status = 'successful';
36+
state.data = action.payload;
37+
})
38+
.addCase(fetchCategories.rejected, (state) => {
39+
state.status = 'failed';
40+
})
4041
}
4142
});
4243

‎src/slices/todosSlice.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
1+
import { createAsyncThunk, createSlice, createAction } from "@reduxjs/toolkit";
22

33
export const fetchTodos = createAsyncThunk(
44
'todos/fetchTodos',
@@ -14,6 +14,9 @@ const initialState = {
1414
data: []
1515
}
1616

17+
// const incrementBy = createAction('incrementBy')
18+
// const decrement = createAction('decrement')
19+
1720
export const todosSlice = createSlice({
1821
name: 'todos',
1922
initialState,
@@ -34,17 +37,18 @@ export const todosSlice = createSlice({
3437
state.data.splice(index, 1);
3538
}
3639
},
37-
extraReducers: {
38-
[fetchTodos.pending]: (state) => {
39-
state.status = 'loading';
40-
},
41-
[fetchTodos.fulfilled]: (state, action) => {
42-
state.status = 'successful';
43-
state.data = action.payload;
44-
},
45-
[fetchTodos.rejected]: (state) => {
46-
state.status = 'failed';
47-
}
40+
extraReducers: (builder) => {
41+
builder
42+
.addCase(fetchTodos.pending, (state) => {
43+
state.status = 'loading';
44+
})
45+
.addCase(fetchTodos.fulfilled, (state, action) => {
46+
state.status = 'successful';
47+
state.data = action.payload;
48+
})
49+
.addCase(fetchTodos.rejected, (state) => {
50+
state.status = 'failed';
51+
})
4852
}
4953
});
5054

0 commit comments

Comments
 (0)
Please sign in to comment.