Created
November 26, 2021 11:33
-
-
Save codewithbernard/69e729b074e979ae9d61ff7e2e35c18d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const initialState = []; | |
| const usersReducer = (state = initialState, action) => { | |
| switch (action.type) { | |
| case "SET_USERS": | |
| return action.payload; | |
| case "ADD_USER": | |
| return [...state, action.payload]; | |
| case `EDIT_USER`: | |
| const newState = [...state]; | |
| const index = newState.findIndex((item) => item.id === action.payload.id); | |
| newState[index] = action.payload; | |
| return newState; | |
| case "DELETE_USER": | |
| return state.filter((user) => item.id !== action.payload.id); | |
| default: | |
| return state; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment