Last active
February 16, 2020 10:02
-
-
Save topherPedersen/5c8f09159496500fbf15b64928d5c224 to your computer and use it in GitHub Desktop.
Broken Redux Reducer
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
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