Last active
January 29, 2017 11:41
-
-
Save pavsidhu/e37c51a359bab6c19f76aa7a019a88ea to your computer and use it in GitHub Desktop.
Redux reducer example
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
import {ADD_TO_COUNTER} from './actions' | |
// This is the default state of the app i.e. when the app starts for the first time | |
const initialState = { | |
counter: 0 | |
} | |
// This is a reducer which listens to actions and modifies the state | |
const reducer = (state = initialState, action) => { | |
// A switch is used since if more actions are added in the future, it will be easy | |
// to be able to handle this in the reducer since we just add another 'case'. | |
switch (action.type) { | |
case ADD_TO_COUNTER: | |
return { | |
...state, | |
counter: state.counter + 1 | |
} | |
default: | |
return state |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment