Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Louiefigz/1097cc02a878f80f0842fb054089734d to your computer and use it in GitHub Desktop.
Save Louiefigz/1097cc02a878f80f0842fb054089734d to your computer and use it in GitHub Desktop.
// 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