Created
May 22, 2017 13:25
-
-
Save Louiefigz/1097cc02a878f80f0842fb054089734d 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
// manageBand is our state manager for Bands. Basically, | |
// instead of making changes in component states, | |
// we're going to be taking care of those state changes here when it | |
// comes to Bands. | |
//counter is just for the unique ID for our Delete case. | |
let counter = 0 | |
export default function manageBand (state = { | |
bands: [] | |
// We can enter manual information into our state like below. | |
// bands: [{text: "manual input here"}] | |
}, action){ | |
switch(action.type){ | |
case "ADD_BAND": | |
counter++; | |
action.band["id"] = counter; | |
// returning an object with bands as a key. | |
return { bands: state.bands.concat(action.band) }; | |
case "DELETE_BAND": | |
const bands = state.bands.filter( band => band.id !== action.id ) | |
return { bands } | |
default: | |
// if we wrapp the below state like { state }, it would have created another object. | |
// we would have to say this.props.getState().state.bands | |
// without the {}, we just call this.props.getState().bands | |
return state | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment