Skip to content

Instantly share code, notes, and snippets.

@codewithbernard
Created November 26, 2021 11:33
Show Gist options
  • Select an option

  • Save codewithbernard/69e729b074e979ae9d61ff7e2e35c18d to your computer and use it in GitHub Desktop.

Select an option

Save codewithbernard/69e729b074e979ae9d61ff7e2e35c18d to your computer and use it in GitHub Desktop.
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