I’m not sure why the bugRemoved action is not working. My code as below.
I thought the last state would have 2 bugs only, but it still has 3.
import { createSlice } from '@reduxjs/toolkit';
let lastId = 0;
const slice = createSlice({
name: 'bug',
initialState: [],
reducers: {
bugAdded: (bugs, action) => {
bugs.push({
id: ++lastId,
description: action.payload.description,
resolved: false,
});
},
bugResolved: (bugs, action) => {
const index = bugs.findIndex((bug) => bug.id === action.payload.id);
bugs[index].resolved = true;
},
bugRemoved: (bugs, action) => {
bugs.filter((bug) => bug.id !== action.payload.id);
},
},
});
export const { bugAdded, bugRemoved, bugResolved } = slice.actions;
export default slice.reducer;
index.js
import configureStore from './store/configureStore';
import {
bugAdded,
bugRemoved,
bugResolved,
getUnresolvedBugs,
} from './store/bugs';
const store = configureStore();
const unsubscribe = store.subscribe(() => {
console.log('store changed', store.getState());
});
store.dispatch(bugAdded({ description: 'bug1' }));
store.dispatch(bugAdded({ description: 'bug2' }));
store.dispatch(bugAdded({ description: 'bug3' }));
store.dispatch(bugRemoved({ id: 1 }));
console.log(store.getState()); //I would have thought the last state has 2 bugs but it still has 3.