createSlicebeginnerconst { createSlice } = ReduxToolkit
const todosSlice = createSlice({
name: 'todos',
initialState: [],
reducers: {
addTodo(state, action) { state.push(action.payload) },
},
})Immer lets you mutate the draft state directly β createSlice handles the immutable update behind the scenes.
createSlice uses Immer under the hood, so you can write "mutating" code that is actually applied immutably. You can push, splice, or assign properties directly.
Challenge: Create a todos slice where:
addTodo pushes a todo to the arrayremoveTodo removes a todo by idtodosSlice.reducer([], addTodo({ id: 1, text: 'buy milk' }))
// β [{ id: 1, text: 'buy milk' }]