I am using a local json file to store some data as the follows:
[
{
"name": "Sports",
"todos": [
{
"id": 1,
"title": "Play Football"
},
{
"id": 2,
"title": "Play Basketball"
}
]
},
{
"name": "Work",
"todos": [
{
"id": 3,
"title": "Study TS"
},
{
"id": 4,
"title": "Work Hard"
}
]
}
]
I made a reducer that takes the two categories above and actually display all these data on the component page.
I am now trying to create another reducer that deletes a certain item from within the todos array that is within each category but I am failing. Here are the actions and the reducers I created so far
// actions
export const getCategories = () => {
return async (dispatch: Dispatch) => {
const response = await axios.get<Category[]>(url);
dispatch({
type: 'GET_ALL_CATEGORIES',
payload: response.data
});
};
};
export const deleteTodo = (id: number) => {
return {
type: 'DELETE_A_TODO',
payload: { id }
};
};
// reducers
const categoriesReducer = (state: Category[] = [], action: Action) => {
switch (action.type) {
case 'GET_ALL_CATEGORIES':
return action.payload;
case 'DELETE_A_TODO':
state.forEach(category => {
return category.todos.filter(
todo => todo.id !== action.payload.id
);
});
default:
return state;
}
};
export const reducers = combineReducers<ReduxStoreState>({
categories: categoriesReducer
});
Then inside the component I wired up the deleteTodo action successfully but nothing gets deleted, there is no difference in the state.
What am I doing wrong?
forEachdoesnt return anything