Skip to content

Instantly share code, notes, and snippets.

@codebucks27
Last active April 22, 2021 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebucks27/aab3cc2991fcb322fd70c4fefb6b0836 to your computer and use it in GitHub Desktop.
Save codebucks27/aab3cc2991fcb322fd70c4fefb6b0836 to your computer and use it in GitHub Desktop.
import { createSlice } from "@reduxjs/toolkit";
const initialState = [];
const addTodoReducer = createSlice({
name: "todos",
initialState,
reducers: {
//here we will write our reducer
//Adding todos
addTodos: (state, action) => {
state.push(action.payload);
return state;
},
//remove todos
removeTodos: (state, action) => {
return state.filter((item) => item.id !== action.payload);
},
//update todos
updateTodos: (state, action) => {
return state.map((todo) => {
if (todo.id === action.payload.id) {
return {
...todo,
item: action.payload.item,
};
}
return todo;
});
},
//completed
completeTodos: (state, action) => {
return state.map((todo) => {
if (todo.id === action.payload) {
return {
...todo,
completed: true,
};
}
return todo;
});
},
},
});
export const {
addTodos,
removeTodos,
updateTodos,
completeTodos,
} = addTodoReducer.actions;
export const reducer = addTodoReducer.reducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment