Skip to content

Instantly share code, notes, and snippets.

@topherPedersen
Last active February 16, 2020 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save topherPedersen/5c8f09159496500fbf15b64928d5c224 to your computer and use it in GitHub Desktop.
Save topherPedersen/5c8f09159496500fbf15b64928d5c224 to your computer and use it in GitHub Desktop.
Broken Redux Reducer
const initialState = {
snacks: []
}
const logCaloriesReducer = (state, action) => {
// check for state undefined to prevent
// redux from crashing app on load
if (typeof state === 'undefined') {
return initialState;
}
switch(action.type) {
case 'LOG_CALORIES':
const updatedState = { ...state };
const indexOfNextSnack = updatedState.snacks.length;
const newSnack = {
calories: action.payload.calories,
time: action.payload.time
};
updatedState.snacks[indexOfNextSnack] = newSnack;
return updatedState;
default:
return state;
return state;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment