Last active
June 3, 2019 12:09
-
-
Save shcyiza/a419bd7fb5df623d674838f1e01770d4 to your computer and use it in GitHub Desktop.
This file contains 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
import { SET_BOOKNAME, ADD_BOOKMARK, DELETE_BOOKMARK } from '../actions/types'; | |
export default function bookmarksReducer(state = {book_name: "", list: []}, action) { | |
switch (action.type) { | |
case SET_BOOKNAME: | |
return {...state, book_name: action.payload} | |
// copy the current state | |
// and change the value of book_name property | |
// with payload property given by the action | |
case ADD_BOOKMARK: | |
return {...state, list:[...state.list, action.payload]} | |
// copy the current state | |
// make a new list out of the list in the current state | |
// concatenate the copied list with the payload of the action | |
case DELETE_BOOKMARK: | |
return {...state, list: state.list.filter(bookmark => bookmark.id !== action.payload.id)} | |
// The Array.filter method returns a new array from the original | |
// Like the Array.maps methods for instance | |
// This is one of the case where JS does immutability OK out of box | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment