Created
August 4, 2018 22:41
-
-
Save minmaxdata/31303ae365c4f6b4afc9b603528d2416 to your computer and use it in GitHub Desktop.
Reducer Example
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
/* Create A Reducer | |
* | |
* You need to create a reducer called "appReducer" that accepts two arguments: | |
* - First, an array containing information about ice cream | |
* - Second, an object with a 'DELETE_FLAVOR' `type` key | |
* (i.e., the object contains information to delete the flavor from the state) | |
* | |
* The action your reducer will receive will look like this: | |
* { type: 'DELETE_FLAVOR', flavor: 'Vanilla' } | |
* | |
* And the initial state will look something like this (as such, refrain | |
* from passing in default values for any parameters!): | |
* [{ flavor: 'Chocolate', count: 36 }, { flavor: 'Vanilla', count: 210 }]; | |
*/ | |
const DELETE_FLAVOR = 'DELETE_FLAVOR'; | |
function myAction(flavor) { | |
return { | |
type: DELETE_FLAVOR, | |
flavor | |
} | |
}; | |
function addTodo(text) { | |
return { | |
type: ADD_TODO, | |
text | |
} | |
} | |
function appReducer (state = {}, action) { | |
if(action.type == DELETE_FLAVOR) { | |
return action | |
} | |
return state | |
} | |
console.log(appReducer({}, myAction(‘Vanilla'))); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment